Missing Data - Save Variable(s) - Complete Cases

From Q
Jump to: navigation, search

Creates a filter variable that includes cases with complete data (i.e., no missing values), based on the variables selected.

Example

The script produces a SUMMARY table with the new filter variable:

Completecases.PNG

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 Selection Functions');
includeWeb('QScript Utility Functions');
includeWeb('JavaScript Array Functions');
includeWeb("QScript Functions to Generate Outputs");

main();
 
function main() {

    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
    var not_applicable_variables = [];
    function variableIsApplicable (q) {
        return !q.isHidden
                && q.questionType != "Text" 
                && q.questionType != "Text - Multi"; 
    }
    // Selecting the variables
    if (!requireDataFile())
        return false;
    var data_file;
    var variables = project.report.selectedVariables();
    variables = splitArrayIntoApplicableAndNotApplicable(variables, variableIsApplicable);
    var selected_variables = variables.applicable;
    if (selected_variables.length == 0)
    {
        if (project.dataFiles.length > 1)
            data_file = selectOneDataFile('There is more than one data file in your project. ' + 
                'Select the data file to use:', project.dataFiles)
        else 
            data_file = project.dataFiles[0];   
        var non_text_variables = data_file.variables.filter(function (v) { return variableIsApplicable(v); });
        if (non_text_variables.length == 0) {
            log("There are no appropriate data in the data file.");
            return false;
        }
        while (!selected_variables || selected_variables.length < 1)
            selected_variables = selectManyVariablesByLabel('Select variables:', non_text_variables).variables;
    }
    if (!selected_variables)
        return false;
    data_file = selected_variables[0].question.dataFile;


    // Create variable
    var expression = "(!isNaN(" + selected_variables[0].name + ") || (typeof " + selected_variables[0].name + " === 'string'))";
    var new_name = "Complete cases: " + selected_variables[0].name;
    var n_variables = selected_variables.length;
    if (n_variables > 1)
        for (var i = 1; i < n_variables; i++)
        {
            expression += " && (!isNaN(" +  selected_variables[i].name + ") || (typeof " + selected_variables[i].name + " === 'string'))";
            new_name += " + " + selected_variables[i].name;
        }
    var new_var = data_file.newJavaScriptVariable(expression, false, 
        cleanVariableName(preventDuplicateVariableName(data_file, new_name)),
        new_name, null);
    new_var.question.isFilter = true;
    new_var.question.questionType = "Pick One";
    new_var.question.valueAttributes.setLabel(0,'Incomplete');
    new_var.question.valueAttributes.setLabel(1,'Complete');

    // Generating outputs
    reportNewRQuestion(new_var.question, "Complete cases");

    return true;
}


See also