Combining a Pick Any Question with a Tie-breaker Pick One Question

From Q
Jump to navigation Jump to search

The following example Expression creates a Categorical Variable measuring most preferred airline from the following questions.

Q8.	Which airlines did you consider flying?  
Jetstar	1
Qantas	2
Rex	3
Virgin Blue	4
Other airlines	5
IF HAS NOT BOOKED ON OWN: Don’t know	6
Q9.	ASK IF MORE THAN 1 AIRLINE SELECTED IN Q8: And which airline did you end up flying? 
ASK IF CONSIDERED JETSTAR: Jetstar	1
ASK IF CONSIDERED QANTAS: Qantas	2
ASK IF CONSIDERED REX: Rex	3
ASK IF CONSIDERED VIRGIN BLUE: Virgin Blue	4
ASK IF CONSIDERED OTHER AIRLINES: Other airline	5

To create a variable for this purpose:

  1. Right-click in the Variables and Questions tab and select Insert Variables > JavaScript Formula > Numeric. For more on creating variables in JavaScript, see JavaScript Variables.
  2. Enter the code below into the Expression.
  3. Modify the code to match the Variable Names of the variables that you need to use in the tie-breaker.
  4. Enter a Name and Label for the variable and click OK.
if (isNaN(q9)) q8_A + 2*q8_B + 3*q8_C + 4*q8_D + 5*q8_E; else q9;

A few things to note:

  • The variable will, by default, be treated as a Numeric Variable. In order to have Q recognize the categories when creating a table, you will need to change the Question Type to Pick One.
  • isNaN(q9) is true for respondents with missing values (i.e., a NaN value) and false otherwise.
  • The code q8_A + 2*q8_B + 3*q8_C + 4*q8_D + 5*q8_ET is only run if the person was not asked Question 9.
  • If the person was asked Question 9, the newly constructed variable will contain a copy of their answer to Question 9.

Equivalently, the same thing can be achieved using else if statements:

if (isNaN(q9) && q8_A == 1) 1;
else if (isNaN(q9) && q8_B == 1) 2;
else if (isNaN(q9) && q8_C == 1) 3;
else if (isNaN(q9) && q8_D == 1) 4;
else if (isNaN(q9) && q8_E == 1) 5;