Create New Variables - Rebase Multiple Response Data in Variable(s) to NET
Create new variable(s) 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.
Example
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.
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
JavaScript
includeWeb('QScript Functions to Generate Outputs');
includeWeb('QScript Selection Functions');
includeWeb('QScript Utility Functions');
if (!main())
log("QScript Cancelled.");
else
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 = project.report.appendGroup();
new_group.name = web_mode ? "Rebased Variable Sets" : "Rebased Questions";
var new_var_names = [];
selected_questions.forEach(function (q) {
var new_question = rebasePickAny(q, hide_inputs);
insertAtHoverButtonIfShown(new_question);
new_var_names.push(new_question.name);
if (!web_mode) {
var t = new_group.appendTable();
t.primary = new_question;
t.cellStatistics = ['%', 'n', 'Base n'];
}
});
// More recent Q versions can point the user to the new items.
if (!web_mode && fileFormatVersion() > 8.65)
project.report.setSelectedRaw([new_group.subItems[0]]);
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 = "(" + variables.map(function (v) { return v.name; }).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, variable.name + "_r");
var rebased_var = data_file.newJavaScriptVariable("if (" + chosen_formula + ") " + variable.name + "; \r\nelse NaN;", false, new_name, variable.label, last, true);
rebased_var.variableType = "Categorical";
rebased_vars.push(rebased_var);
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, question.name + " - rebased to NET"),
question.questionType, // Pick Any or Pick Any - Grid
rebased_vars);
// 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
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.
Displayr - Anything Menu
Displayr - Insert
Displayr - New Variable Menu
Extensions
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Data Cleaning QScripts
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library
User Interface > Transformation