This example QScript includes a function for rebasing variables.
Method
function rebaseQuestion(question_name, condition) {
var x = project.dataFiles[0].getQuestionByName(question_name);
var q_variables = x.variables;
var qt = x.questionType;
var is_text = qt == "Text" || qt == "Text - Multi"
var rebased_vars = [];
var last = q_variables[q_variables.length-1];
// For each variable, create the appropriate rebased variable.
for (var j=0;j<q_variables.length;j++) {
var expression = "if (" + condition + ") "+ q_variables[j].name.toString() + "; else NaN";
var rebased_var = project.dataFiles[0].newJavaScriptVariable(expression, is_text, q_variables[j].name.toString() + "rebased",q_variables[j].label.toString() , last);
rebased_vars.push(rebased_var);
last = rebased_var;
}
var new_question = project.dataFiles[0].setQuestion(x.name + " - rebased", qt, rebased_vars);
if (!is_text)
copyValueAttributesForQuestion(x, new_question );
project.dataFiles[0].moveAfter(x.variables,project.dataFiles[0].variables[project.dataFiles[0].variables.length-1]);
x.isHidden=true;
log("The question: \"" + x.name + "\" has been rebased using the condition " + condition + ".");
}
//Copies the value attributes from one variable to another
function copyValueAttributesForVariable(from_variable, to_variable){
var n = from_variable.uniqueValues
var values = from_variable.uniqueValues;
var qt = to_variable.question.questionType;
var qt_old = from_variable.question.questionType;
if (qt != qt_old)
alert("Question types are different.");
for (var i=0; i<values.length; i++){
var v = values[i];
if (qt != "Text" && qt != "Text - Multi"){
to_variable.valueAttributes.setLabel(v,from_variable.valueAttributes.getLabel(v));
to_variable.valueAttributes.setIsMissingData(v,from_variable.valueAttributes.getIsMissingData(v));
if (qt == "Pick Any" || qt == "Pick Any - Compact" || qt == "Pick Any - Grid")
to_variable.valueAttributes.setCountThisValue(v,from_variable.valueAttributes.getCountThisValue(v));
else
to_variable.valueAttributes.setValue(v,from_variable.valueAttributes.getValue(v));
}
}
}
//This function copies attributes, variable by variable, from one question to another
function copyValueAttributesForQuestion(from_question, to_question){
var x = from_question.variables;
var y = to_question.variables;
if (y.length != x.length)
alert('Questions contain different numbers of variables.');
for (var j=0;j<x.length;j++)
copyValueAttributesForVariable(x[j], y[j])
}
Note, the rebaseQuestion
function calls two other functions which need to also be included: copyValueAttributesForVariable
and copyValueAttributesForQuestion
.
Example
The below example filters a series of variable sets based on rules relating to Q2 and Q3:
rebaseQuestion("Q5. Brand associations","Q3 <= 4 && Q2 == 1")
rebaseQuestion("Q10. Why drinks more than one cola","Q3 <= 4 && Q2 == 1")
rebaseQuestion("Q11. Main difference between cola drinkers","Q3 <= 4 && Q2 == 1")
rebaseQuestion("Q6. Brand attitude (numeric -100 to 100)","Q3 <= 4 && Q2 == 1")
rebaseQuestion("Q1. Fragments coded","Q3 <= 4")
rebaseQuestion("Q2. Gender","Q3 <= 4 && Q2 == 1")
rebaseQuestion("Q3. Age","Q3 <= 4")
rebaseQuestion("Q4. Frequency of drinking","Q3 <= 4")
Note, the function code under the Method section needs to additionally appear within your script when running this.