Modify Data - Remove Don't Know Categories

From Q
Jump to navigation Jump to search

Remove "Don't Know" categories from variable sets in your document

This QScript removes Don't Know categories from questions in your project. It is the equivalent to right-clicking on all the Don't Know categories of the selected questions and selecting Remove.

Technical details

This QScript:

  1. Looks through all of the Pick One and Pick One - Multi questions in the project and identifies any questions that contain Don't Know options.
  2. Presents the user with a list of questions that contain Don't Know options and asks them choose the questions to remove these categories from.
  3. Creates a new folder in the report containing a table for each question that has been changed.

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.

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

// Script to remove 'Don't Know' options from Pick One and Pick One - Multi questions.
 
includeWeb('QScript Utility Functions');
includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Value Attributes Functions');
includeWeb('QScript Questionnaire Functions');
includeWeb('QScript Selection Functions');
includeWeb('JavaScript Array Functions');
 
if (!main()) 
    log("Qscript Cancelled.");
else
    conditionallyEmptyLog("QScript Finished."); 
 
function main() {
    var selected_questions;
 
    var extra_dk_strings;
 
    // On the web just take from what is selected.
    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
 
 
    if (web_mode) {
        selected_questions = project.report.selectedQuestions();
 
        // Split out any selected questions that are of the wrong type
        var question_type_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, function (q) { return q.questionType.indexOf("Pick One") == 0; });
        var wrong_type = question_type_selection.notApplicable;
        selected_questions = question_type_selection.applicable;
 
        // Split out questions that don't have a 'Dont Know' option
        var sorted_selection = splitArrayIntoApplicableAndNotApplicable(selected_questions, questionHasNonMissingDontKnowOption)
        selected_questions = sorted_selection.applicable;
        var not_applicable_questions = sorted_selection.notApplicable;
        extra_dk_strings = [];
    } else {
        // Prompt the user to enter additional labels for DK options
        extra_dk_strings = promptForDKLabels();
 
        var selected_datafiles = dataFileSelection();
 
        // Get all Pick One and Pick One - Multi questions that look to have 'Dont Know' options
        var relevant_questions = getDKQuestionsWithUserEnteredLabels(["Pick One", "Pick One - Multi"], extra_dk_strings);
 
        if (relevant_questions.length == 0) {
            log("No appropriate questions found.");
            return false;
        }
 
        // Ask the user which questions they want to change
        selected_questions = selectManyQuestions("Select the questions for which you would like to remove the 'Don't Know' option.", relevant_questions).questions;
    }
 
    var num_selected = selected_questions.length;
    var current_value_attributes;
    var current_unique_values;
    var current_question;
    var num_vals;
    var labels;
    // Set 'Dont Know' options in the selected questions as missing
    for (var j = 0; j < num_selected; j++) {
        current_question = selected_questions[j];
        current_value_attributes = current_question.valueAttributes;
        current_unique_values = current_question.uniqueValues;
        num_vals = current_unique_values.length;
        labels = valueLabels(current_question);
        for (var k = 0; k < num_vals; k++) {
            if (isDontKnow(labels[k]) || containsSubstring(labels[k], extra_dk_strings))
                current_value_attributes.setIsMissingData(current_unique_values[k], true);
        }
    }
 
    if (web_mode) {
        if (selected_questions.length > 0)
            logQuestionList("Modified:", selected_questions)
 
 
        if (wrong_type.length > 0) {
            if (selected_questions.length > 0)
                log("\r\n")
            logQuestionList("Data is of the wrong type:", wrong_type);
        }
 
        if (not_applicable_questions.length > 0) {
            if (selected_questions.length > 0 || wrong_type.length > 0)
                log("\r\n");
            logQuestionList("Could not find a 'Don't Know' option:", not_applicable_questions);
        }
 
    } else {
        // Generate a group containing tables for the questions that have been changed.
        generateGroupOfSummaryTables("Questions with 'Don't Know' options removed", selected_questions);
        log("Questions that have been modified are shown in the folder: Questions with 'Don't Know' options removed");    
    }
 
    return true;
}
 
 
function splitArrayIntoApplicableAndNotApplicable(array, my_function) {
    var applicable = [];
    var not_applicable = [];
    array.forEach(function (x) {
        if (my_function(x))
            applicable.push(x);
        else
            not_applicable.push(x);
    });
    return {applicable: applicable, notApplicable: not_applicable};
}

Prior to the 15th of December, 2015, this page was known as Modifying Rows and Columns - Remove Don't Know Categories

See also