Recode - Set Value of Don't Knows to NaN

From Q
Jump to navigation Jump to search

Set the value of any Don't Know categories to NaN This QScript sets the value of any Don't Know categories in Pick One, Pick One - Multi, Number, and Number - Multi questions to NaN.

Optionally, you can choose to create new tables that display the number of respondents who have selected a Don't Know option in the selected questions.

Technical details

Phrases that are identified as Don't Know type responses are ones that contain one or more of:

  • dk
  • dont know
  • don't know
  • don t know
  • unsure
  • not sure
  • do not know
  • no idea
  • not applicable

The matching of these is not case sensitive.

Additionally, where a label is, in its entirety, NA or na, it is treated as a Don't Know.

Questions modified by this QScript will be shown in a new folder in the report called Recoded Questions.

If any of the questions are seen to have more than one Don't Know option then these will be added to another folder called Questions to check.

How to apply this QScript

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Click on the QScript when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Select this QScript from the list.

Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

Customizing QScripts in Q4.11 and more recent versions

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
  • Press Edit a Copy (bottom-left corner of the preview).
  • Modify the JavaScript (see QScripts for more detail on this).
  • Either:
    • Run the QScript, by pressing the blue triangle button.
    • Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

Customizing QScripts in older versions

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.

JavaScript

includeWeb('QScript Utility Functions');
includeWeb('QScript Questionnaire Functions');
includeWeb('QScript Selection Functions');
includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Value Attributes Functions');
 
if (!main()) 
    log("Qscript Cancelled.");
else
    log("QScript Finished.");
 
function main() {

    // Prompt the user to enter additional labels for DK options
    var extra_dk_strings = promptForDKLabels();

    // Get the user to select from questions containing DK options
    var relevant_questions = getDKQuestionsWithUserEnteredLabels(["Pick One", "Pick One - Multi", "Number", "Number - Multi"], extra_dk_strings);
 
    if (relevant_questions.length == 0) {
        log("No appropriate questions found.");
        return false;
    }
 
    var selected_questions = selectManyQuestions("Select the questions to modify:", relevant_questions).questions;
 
    if (selected_questions.length == 0) {
        log("No questions selected.");
        return false;
    }
 
    // Modify the selected questions
    var questions_to_check = []; // Questions with more than 1 don't-know should be inspected by the user
    var easy_questions = [];
    selected_questions.forEach(function (q) {
        var value_attributes = q.valueAttributes;
        var change_counter = 0;
        var dk_values = [];
        q.uniqueValues.forEach(function (v) {
            var cur_label = value_attributes.getLabel(v); 
            if (isDontKnow(cur_label) || containsSubstring(cur_label, extra_dk_strings) ) {
                setValueForVariablesInQuestion(q, v, NaN);
                dk_values.push(v);
                change_counter++;
            }
        });
        if (change_counter > 1)
            questions_to_check.push(q);
        else
            easy_questions.push(q);
    });
 
    var new_group_name = "Recoded Questions";
    var new_group = generateGroupOfSummaryTables(new_group_name, easy_questions);
 
    log("Recoded questions have been added to a group in your Report: " + new_group_name);
 
    if (questions_to_check.length > 0) {
        var check_group_name = "Questions to check";
        log("Some questions appear to have more than one Don't Know category and should be checked. These have been placed in the group: " + check_group_name);
        generateSubgroupOfSummaryTables(check_group_name, new_group, questions_to_check);
    }

    return true;
}

See also