This example shows how to create a recoded version of specific variable sets using a QScript.
Method
This script uses Colas.sav, and recodes three existing variables into three new variables, so that values of 6 and above are assigned a value of 1, and values less than 6 are assigned a value of 0. This example would be much better and faster done by changing the Question Type or Structure of the variable set to a Pick Any/ Binary - Multi (i.e., this example is presented to illustrate key concepts that are generally applicable).
var data_file = project.dataFiles[0];
var q4_vars = data_file.getVariablesByName("q4")
var n_vars = q4_vars.length;
for (var i=n_vars-1; i>=0; i--){
var var_name = q4_vars[i].name;
data_file.newJavaScriptVariable("if ("+ var_name +" >= 6) 1; else 0", false, "Q4TopTwo" + (i+1), q4_vars[i].label + "
weekly",null);
}
// Setting a question
data_file.setQuestion('Q4 Top 2 Boxes', 'Pick Any', data_file.getVariablesByName("Q4TopTwo"));
// Creating a frequency table
var t = project.report.appendTable();
t.primary = data_file .getQuestionByName("Q4 Top 2 Boxes");
t.secondary = "SUMMARY";