Rebasing Questions Using a Condition

From Q
Jump to navigation Jump to search

This script calls two other functions which will need to be included in order for this script to run: Copying Value Attributes for a Variable and Copying Value Attributes for a Question. Only the lines at the end need to be modified.

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 + ".");
}

//This example filters a series of question based on rules relating to Q2 and Q3 
// (the lines below should be modified to match your data)
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")

See also