In this article
This article contains a step-by-step example of how you recode a single question using the Expression Builder functionality. The procedure for other types of questions is the same though the number of functions available in the Expression Builder may be different (go to Functions in the Condition Builder for more information). Note that you can recode questions "manually" (go to Recoding Questions Manually for more information), but the Expression Builder guarantees correct syntax.
For this example, assume you have the following answers in your Age question - :
- Under 18 (code 1)
- 18 to 30 (code 2)
- 31 to 40 (code 3)
- 41 to 50 (code 4)
- 51 to 60 (code 5)
- Over 60 (code 6)
as shown below.
Figure 1 - The answer options before recoding
And you would like to recode these to "Up to 40" (codes 1, 2 and 3) and "Over 40" (codes 4, 5 and 6).
To recode the question in Authoring:
- Open the survey containing the question you wish to recode.
- Add a Single question to the Survey, after the question you wish to recode, and give it a logical name - .
- Go to the question's Answers tab and add the answer alternatives Up to 40 and Over 40 to the question - .
- Open the question's Properties page, click Show advanced view, then click the down-arrow beside the Variable Type field and select the Recoded variable option from the list - . See the note below.
- Save the changes.
Figure 2 - The new "Recoding" question in the Survey tree
Figure 3 - The answer alternatives for the recoding question
Note: From Version 16 you will no longer be able to launch a survey that includes legacy recoded variables. In the event you attempt to launch such a survey, a message will be displayed informing you of what you will need to do. It will still be possible to use existing surveys that include legacy recoded variables in Reportal and Data Exports, but it will not be possible to relaunch these surveys, or to calculate legacy recoded variables for any new respondents.
Figure 4 - Selecting Recoded Variable
The options in the Properties pane change, several of the columns in the question's Answers list are removed and the Expression column appears - .
Figure 5 - The Answers tab when Variable Type is set to Recodes Variable
You must now add expressions to the fields in the Expression column. These expressions must be programmed as Boolean, evaluating to true or false.
Note: When basing a variable on another specific question you can use the Expression Builder to assist you with creating the expressions.
- Click in the Expression field for the first recoded answer, then click on the question in the Survey tree that you wish to recode (in this case the Age question).
- Click in the "Up to 40" Expression field.
- For "Up to 40" you can say "if age is code 1 or if age is code 2 or if age is code 3", and similar for "Over 40".
- For "Up to 40" you can say "if age is less than or equal to code 3" and for "Over 40", "if age is greater than or equal to code 4".
- In this case, click into the "Up to 40" Expression field, then select age = Under 18 [1]and click Add to expression.
- Select the Or operator, then select age = 18 to 30 [2]and again click Add to expression.
- Repeat for the 31 to 40 value.
- Create the expression for the "Over 40" answer in the same way.
- Save the changes.
Note: When creating an expression, you must select questions that are at the same "level" in the Survey Tree. For example, if you are creating a recoded question within a loop, then you must select from the questions within that loop - you cannot select a question that is outside of that loop.
The Expression Builder opens below the Answers tab, containing the answer options from the question to be recoded. - .
The yellow Click here... button opens a new window with descriptions of all the functions available in the expression builder (go to Functions in the Condition Builder for more information).
Figure 6 - The Expression Builder below the Answers tab, with the age question and its answers displayed
In the Expression Builder, age is already selected. You now have several options for creating the expression:
Note: If you notice a mistake before you have saved the changes, you can correct this by using the Delete or Backspace keys on you keyboard or by clicking the Clear button in the Expression field. On completion, click Validate to check that your result is correct.
The result should be - :
Figure 7 - The result for the first part of the expressionFigure 7 -
The final result should be - :
Figure 8 - The resulting expressions
Note: In the event you find an error in an expression after you have saved, you can delete the expression by selecting the field in the Answers tab and clicking Clear.
Note: After adding a re-coded variable, you must always re-launch the survey using either the Incremental or the Rebuild option.
In addition to the forms in the Survey, you can also use the system-generated variables interview_start, interview_end and status in recoded variables.