This article describes how to create a set of dynamic variables for the current and previous waves based on the period selected in a control box:
- A data set that includes a Nominal wave variable. In this example, we have a variable called Wave that tells us what quarter the survey was completed.
- A Control that includes the items from the wave variable. Here, we have data for Qtr 1 to Qtr 4. As we are interested in the current and previous waves, we have omitted Qtr 1 from our wave.combo.box control.
1. Select your data set in the Data Sets tree.
2. Go to Anything > Data > Variables > New > Custom Code > R - Numeric in the toolbar.
3. Paste the below under Properties > R CODE in the object inspector:
waves = Wave
unique.waves = levels(waves) #Get wave categories
curr.index = match(wave.combo.box,unique.waves) #Find index of current wave
waves %in% unique.waves[curr.index-1] #Return TRUE for previous wave
- The above code gets the levels or categories from the Wave variable and defines them as unique.waves.
- It then finds the index of the current wave selected in the control box within unique.waves. Note, this code assumes the waves are in the correct order.
- Finally, we return TRUE or FALSE based on whether the data matches the previous wave category.
4. Update Name and Label under Properties > GENERAL to Previous, for example.
5. Press Duplicate in the toolbar to copy this variable.
6. Replace the R code in the duplicated variable with the below:
Wave %in% wave.combo.box
This will simply return TRUE if the data in our Wave variable matches the category selected in the control box.
7. Update Name and Label under Properties > GENERAL to Current, for example.
8. Select both variables in the Data Sets tree, right-click > Combine.
9. Update the variable set Label in the object inspector to Waves, for example.
10. Change Structure to Binary - Multi: Non-mutually exclusive categories.