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

main();
 
function main() {
    // On the web just take from what is selected.
    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
    var selected_variables;
    var not_applicable_variables = [];
    function variableIsApplicable (q) {
        return !q.isHidden
                && q.questionType != "Text" 
                && q.questionType != "Text - Multi"; 
    }
    // Selecting the variables.
    var data_file;
    if (web_mode) { // Displayr
        var variables = project.report.selectedVariables();
        if (variables.length == 0) {
            log("Select Variables in 'Data Sets'.");
            return false;
        }
        data_file = variables[0].question.dataFile;
        variables = splitArrayIntoApplicableAndNotApplicable(variables, variableIsApplicable);
        selected_variables = variables.applicable;
        not_applicable_variables = variables.notApplicable;
    } else { // Q 
        // Identifying the data file (if there are 2 or more).
        if (project.dataFiles.length == 0) {
            log("There is no data file.");
            return false;
        }
        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];   
        // Show the user an appropriate list variables 
        // Get Pick One - Multi questions
        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;
        }
        // letting the user select the questions to flatten
        while (!selected_variables || selected_variables.length < 1)
            selected_variables = selectManyVariablesByLabel('Select variables to use in the filter:', non_text_variables).variables;
    }
 
    // Create variable
    var expression = "(!isNaN(" + selected_variables[0].name + ") || (typeof " + selected_variables[0].name + " === 'string'))";
    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'))";
    var new_var = data_file.newJavaScriptVariable(expression, false, preventDuplicateVariableName(data_file, "completeCases"), "Complete cases", 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
    if (!web_mode) {
        var new_group = generateGroupOfSummaryTables("Complete cases", [new_var.question]);
        project.report.setSelectedRaw([new_group.subItems[0]]);
    }

    return true;
}


See also