Recoding Into New Variables

From Q
Jump to: navigation, search

The script 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 of the question to a Pick Any (i.e., this example is presented to illustrate key concepts that are generally applicable).

This script assumes you have already imported the data file c:\Program Files\Q\Examples\cola.sav (this may be located on a different place on your computer depending upon how Q was installed). The following script is located in the file: c:\Program Files\Q\Examples\Recoding into new variables.QScript:

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";

Key aspects of this code to note are:

  • The code var data_file = project.dataFiles[0]; is creating a temporary variable which representing the data file. Note that in this context a variable has a much more general meaning than a column of numbers in a data file.
  • var q4_vars = data_file.getVariablesByName("Q4_") is creating a new variable that contains all of the variables in the data file that commence with Q4. In this example, there are only three variables with this common element to their name. This variable is an Array (i.e., it contains multiple variables from the data file).
  • var n_vars = q4_vars.length; counts up the number of variables in the array and assigns it to a temporary variable called n_vars.
  • for (var i=n_vars-1; i>=0; i--) is a loop that first creates a temporary variable called i of value of 1 less than the value of n_vars, then it does everything in the parentheses, after which it reduces the value of i by 1 and does everything in the parentheses again. This cycle is repeated this until i has a value of 0. Note that this loop is decreasing through the variables, starting with the last first and thus ensuring they appear in the correct order (see the last comment on the “simplest” code above).
  • The loop is performed for everything between { and }.
  • var_name = q4_vars[i].name;> creates a new temporary variable containing the Name of the variable that appears in position i in the array called q4_vars.
  • data_file.newJavaScriptVariable tells Q to create a new JavaScript Variable.
  • The expression if ("+ var_name +" >= 6) 1; else 0 returns a value of 1 if the variable has a value of 6 or more and 0 otherwise.
  • The false tells Q not to make a Text variable.
  • "Q4TopTwo" + (i+1) creates a new variable Name for each variable.
  • q4_vars[i].label + " weekly" creates a Label for the variable which is the original variable’s Label with weekly appended.
  • null tells Q to put the variable in the default position, which is at the top of the project (alternatively, we can enter the name of the variable that we wish our new variable to appear beneath in the Variables and Questions tab).
  • The remaining lines set a question and create a frequency table (see Creating a Frequency Table).

See also