Recode - Turn Off Missing Data Selection for Specific Values

From Q
Jump to navigation Jump to search

This tool turns off the Missing Data setting for a particular value (or values) in every variable in your data set. It is generally useful if your data set has been created poorly, with missing data settings that have result in specific code values (e.g. 0) not showing up in your tables.

Usage

  1. Select Automate > Browse Online Library > Recode > Turn Off Missing Data Selection for Specific Values
  2. Type in the values which should be recoded, with multiple values separated with commas
  3. Choose which questions you wish to modify from the list presented.
  1. Select one or more variable sets under Data Sets.
  2. Select + Anything > Data > Variables > Modify > Recode > Turn Off Missing Data Selection for Specific Values
  3. Type in the values which should be recoded, with multiple values separated with commas

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 Selection Functions");

turnOffMissingValues()

function turnOffMissingValues() {
    const web_mode = inDisplayr();

    let selected_questions = [];
    if (web_mode){
        const user_selections = getAllUserSelections();
        selected_questions = user_selections.selected_questions;
        if (selected_questions.length == 0) {
            log("Please select one or more variable sets under Data Sets and run this option again.");
            return false;
        }   
    } else {
        // In Q just get all questions
        project.dataFiles.forEach(function (data_file) {
            selected_questions = selected_questions.concat(data_file.questions);
        })
    }

    selected_questions = selected_questions.filter(function (q) { return q.questionType.indexOf("Text") == -1 && !q.isBanner});
    if (selected_questions.length == 0) {
        log("None of the " + (web_mode ? "selected variable sets" : "questions in your data") + " are appropriate.");
        return false;
    }
    

    // Prompt the user to enter values to untick and make sure they
    // give a sensible input.
    let entered_values;
    let got_valid_values = false;
    let message = (web_mode ? "Enter value(s) which should be set to 'Include in analyses'" : "Enter value(s) to un-tick as Missing Data") + " (separate multiple values with commas):"
    while (!got_valid_values) { 
        let input_string = prompt(message);
        entered_values = input_string.split(",");
        entered_values = entered_values.map(function (s) { return s.trim(); });
        if (!entered_values.every(function (s) { return s == "NaN" || parseFloat(s) == s; })) {
            alert("Enter numeric values or NaN only.");
        } else {
            got_valid_values = true;
        }
    }

    // Convert to numeric
    entered_values = entered_values.map(parseFloat);

    selected_questions.forEach(function (q) {
        let uniques = q.uniqueValues;
        entered_values.forEach(function (value) {
            // Check NaN values separately
            if (isNaN(value) && uniques.some(isNaN)
                || uniques.indexOf(value) != -1) {
                setIsMissingDataForVariablesInQuestion(q, value, false);
            }
        });
    });

    return true;
}

See also