Sometimes, your survey data may contain information about the date the respondents completed the survey stored as categories rather than as date values. Suppose you have a variable in your data file called Week that encodes the week of the survey in Categorical format. This article shows you how to create a new JavaScript variable which has date values derived from categories, going from this:
Requirements
Data collected over time (e.g. quarters, weeks, or some other ordinal information). For example:
Method
Suppose you have a categorical variable called “wave”. It encodes the wave as (1 = wave1, 2 = wave2, 3 = wave3, and 4 = wave4). You want to turn “wave” into a proper date-time variable, to use it as a Date question. This is where a JavaScript variable sorts you out.
You can make a JavaScript variable by
- Create > Variables and Questions > Variables > JavaScript Formula > Numeric (this is also available via a right-click in the Variables and Questions tab).
- In the JavaScript Variable expression window, you’ll use a simple IF AND ELSE IF expression combined with Q’s Q.EncodeDate() function, like the following:
if (wave == 1) Q.EncodeDate(2018,01,31);
else if (wave == 2) Q.EncodeDate(2018,02,28);
else if (wave == 3) Q.EncodeDate(2018,03,31);
else if (wave == 4) Q.EncodeDate(2018,04,30);
In this case, we’re reassigning each of the wave variables to the last day of the month in 2018 from Jan to April respectively. You could set it to any date, as long as it’s in the format (yyyy,mm,dd). As with all JavaScript variables, be sure it checks out in the Preview Panel at the bottom of the window. Push OK to save the variable. Our video on JavaScript variables demonstrates this process.
You now have a new numeric variable in your Variables and Questions tab. Change the variable type to “Date/Time” and you’ll now have a new Date/Time question. Be sure to check the Values to set your desired aggregation.
This Date/Time question will now be available for you to use in your cross-tabs. And, pertinently, for use with the statistical testing against a previous period!
Q will automatically set the labels of the new date variable. If you'd like to change them, you can use a custom Rule to do so. See: How to Relabel a Date/Time Question in a Table in Q
Next
How to Compare Results to the Previous Time Period