In this article
The f function is used to access survey variable values, i.e. the answers given to a question. It will return values or sets depending on the type of variable it is used on.
f('qID'
Its basic syntax is f("qID"), where qID is the question ID specified in Properties (go to Question Properties Overview for more information). The values returned are different for different question types.
Question type |
Values returned |
Single |
The code of the answer. |
Open, Numeric and Date |
A string with the answer. |
Multi |
A set with the codes of the answers selected. |
Ranking, OpenText List or Numeric List |
A set with the codes of the elements in the answer list that are answered. |
Grid |
A set with the codes of the elements in the answer list that are answered. |
"Other" from answer lists |
A string with the text answered. |
Single
Figure 1 - Example of an answer list for a single question "gender"
If the respondent answers Male, then the code f("gender") will return the value M.
Open Text
Figure 2 - Open Text question "name”
In this case, the code f("name") will return John Smith.
Multi
Figure 3 - Example of an answer list for a multi question "TVchannels"
If the respondent answers ABC, CNN and NBC, then the code f("TVchannels") will return the set 1,3,5.
Ranking or Open Text List
Figure 4 - Example of a Ranking question
For a Ranking question, the respondent must provide consecutive answers in the range from 1 to the number of items in the list (in this case 3). The code f("rank") will return the codes of the items answered in the list, 1,3,5.
Figure 5 - Example of an Open Text List question "program"
An Open Text List question will present the answer list with a text box after each item. If Not required is also checked, then the respondent will not have to answer the question. If the question is answered as in the example above, the code f("program") will return a set containing 1,5 (the codes of ABC and NBC are 1 and 5).
(Note that the answer lists of these two questions as well as the grid in next question are filtered based on the answers given on the multi question TVchannels(go to Code and Scale Masks for more information)
Grid
Figure 6 - Example of the grid "hours"
Here the code f("hours") will return a set of codes from the answer list that has an answer, here it would be 1,3,5.
Other, Specify
Figure 7 - Example of the answer list for the single question "favorite"
Figure 8 - How the respondent will see the single question "favorite"
An Other text box will have the "question id"+_+"code"+_other as its variable name. In this case it would be favorite_6_other. The code f("favorite_6_other") will in here return the string MTV.
This syntax is useful when you wish to include the value the respondent enters into the Other text box as an answer alternative in a later question or in a loop.
Figure 9 - Other, specify in loops
In this case the text MTV will appear in the loop, not Other, specify.
f('qID')['code']
Multi, Open Text List, Numeric List, Ranking and Grid questions group one or more variables in the same form. The answers are stored in consecutive columns in the database (go to Question Properties Overview for more information). To extract the answer from one item of a question of one of these types, use as the identifier the code of the item you wish to extract: f("qID")["code"]. The values returned from f("qID")["code"] for different question types are shown below:
Question type |
Values returned |
Multi |
1 if the item with this code is checked off, 0 if it is not. |
Ranking |
A string with the value (rank) given to the item with this code. |
OpenText List |
A string with the text answered for the item with this code. |
| Numeric List | A string with the number answered for the item with this code. |
Grid |
The code (from scale) of the answer to the item with this code. |
Multi
The multi question TVchannels where ABC, CNN and NBC are selected, f("TVchannels")["1"] will return 1 (since ABC is checked), f("TVchannels")["2"] will return 0 (since CBS is not selected).
Ranking
The question rank where ABC was ranked as number 2, CNN as 3 and NBC as number 1, f("rank")["1"] will return 2 (because ABC was ranked as number 2), f("rank")["3"]will return 3 (CNN) andf("rank")["5"]will return 1 (NBC).
Open Text List
f("program")["1"] will return NYPD Blue, f("program")["3"] will return an empty string (because the respondent did not answer anything for CNN) and f("program")["5"] will return Friends.
Grids
f("hours")["1"] will return 2. f("hours")["3"] will return 1 and f("hours")["5"] will return 3 provided that the scale of the grid is defined - .
Figure 10 - Scale of grid "hours"
Loops - f('qID','iteration_code','iteration_code',…..)
You refer to questions inside loops as described above, but you may want to be able to refer to the value in a specific iteration within a loop from another iteration or outside the loop.
For example, assume we have a loop that iterates through a list of channels in a multi question TVchannels. For each channel, a multi question Types is asked, and the respondent checks off different types of programs he/she likes to watch on that channel. Inside this loop there is a new loop iterating through the types of programs the respondent has checked off, and inside that loop the respondent is asked to rate the channel's programs of that type. In addition, if he/she awards a program a score 4 or 5, he/she is asked to give reasons for awarding that score.
Figure 11 - Nested loops
As you see from the IF-condition within the innermost loop - , rating may be referenced as a normal single question, using f('rating'). This expression will give the value of rating within the current iteration. But if you want the value of rating in a different iteration, the "code" of that iteration must be specified. f("rating","1") will give the value of rating of "Sit-coms" (see the figure here below) as this is the first iteration.
Figure 12 - The inner loop, "programs"
This expression may be used within both loops. For the outer loop, the current value for "channels" will be used. However you may also specify the iteration of the outer loop (outside the loops, you must!).
f("rating","1","5") will give the rating of NBC's "Sit-coms" (see the figure here below). Note the order - the innermost loop first. An expression such as f("rating") outside the loops would not make much sense.
Figure 13 - The outer loop, "channels"
Inside the loops you may refer to the current iteration with the syntax f("loopID"), for example f("channels") or f("programs"). The value returned will be the code of the current iteration (go to Text Substitution/Response Piping for more information).