Create New Variables - Rebase Multiple Response Data to NET

From Q
Jump to: navigation, search

Create new variables that adjust for respondents that skipped questions, but were not recorded as missing data, so that the new 'NET' will be 100%.

This QScript is used to rebase Pick Any and Pick Any - Grid questions which do not have missing values recorded for respondents who skipped the question in the questionnaire, leading to a NET which is less than 100%. Any respondents who do not have at least one selection in the question will be removed from the base, and so the NET will be 100%.

The QScript makes a new copy of any questions that you choose to rebase, and you have the option of hiding the original questions if you no longer want to see them in your project.


This example shows a SUMMARY table of an Unaided awareness question. As indicated by the NET, some respondents have not given any answers, but they have also not been recorded as missing data. If those respondents skipped the question in the questionnaire then we may wish to rebase the question to only respondents with at least one answer. The table to the right shows the result of rebasing using this QScript.


Technical details

This QScript is intended to be used for multiple response data that has no Missing Data code. This is to correct for data files which have been created poorly, and it is usually better to ask the data provider to make sure that respondents who skip a question are marked as missing, according to the recommended specifications for SPSS data files, linked here: SPSS Data File Specifications.

If the script is applied to questions which already have missing data then respondents with no responses will still be removed from the base, but the base will not necessarily be the same as the original NET.

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.


  • 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.


includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Selection Functions');
includeWeb('QScript Utility Functions');

if (!main())
	log("QScript Cancelled.");
	conditionallyEmptyLog("QScript finished.");

function main() {
    var allowed_types = ["Binary - Multi", "Binary - Grid"];
    var selected_questions = selectInputQuestions(allowed_types);
    if (!selected_questions)
        return false;
    var hide_inputs = askYesNo("Do you want to hide the original questions?");

    // Make new questions and show them in table
    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
    var new_group =; = web_mode ? "Rebased Variable Sets" : "Rebased Questions";
    var new_var_names = [];
    selected_questions.forEach(function (q) {
    	var new_question = rebasePickAny(q, hide_inputs);
        if (!web_mode) {
            var t = new_group.appendTable();
            t.primary = new_question;
            t.cellStatistics = ['%', 'n', 'Base n'];
        }else { 
            var page = new_group.appendPage('Blank');
            var t = page.appendTable();
            t.primary = new_question;
            t.cellStatistics = ['%', 'n', 'Base n'];

    // More recent Q versions can point the user to the new items.
    if (fileFormatVersion() > 8.65)[new_group.subItems[0]]);
    if (web_mode)
              + "Tables showing the rebased variable sets have been added to the bottom of the document.");
    return true;

// Create a new question where each of the new variables is missing for
// respondents that made no selections in the input question.
function rebasePickAny(question, hide_inputs) {
    var variables = question.variables;
    var num_vars = variables.length;
	var last = variables[num_vars-1];
    var data_file = question.dataFile;

    // Formula for respondents with at least one answer
    var chosen_formula = "(" + (v) { return; }).join(" == 1) || (") + " == 1)";

    // For each variable, create the appropriate rebased variable with a new JavaScript variable
    var rebased_vars = [];
    variables.forEach(function (variable) {
        var new_name = preventDuplicateVariableName(data_file, + "_r");
        var rebased_var = data_file.newJavaScriptVariable("if (" + chosen_formula + ") " + + "; \r\nelse NaN;", false, new_name, variable.label, last, true);
        rebased_var.variableType = "Categorical";
        last = rebased_var;
    // Rename and hide the original question
    if (hide_inputs)
    	question.isHidden = true;

    // Set the question
    var new_q = data_file.setQuestion(
		preventDuplicateQuestionName(question.dataFile, + " - rebased to NET"),
		question.questionType, // Pick Any or Pick Any - Grid

    // Set the Value Attributes
    setCountThisValueForVariablesInQuestion(new_q, 1, true);
    setLabelForVariablesInQuestion(new_q, 1, 'Selected')
    setLabelForVariablesInQuestion(new_q, 0, 'Not Selected')

    return new_q;

See also