Creating a Date Variable from Categories

From Q
Jump to navigation Jump to search

Sometimes your survey data may contain information about the date the respondents completed the survey stored as categories rather than as date values. Using the categories to assign date values to the respondents can allow you make use of some of Q's date aggregation and time series analysis functionality. This article shows you how to create a new JavaScript Variable which has date values derived from categories. Note that it is not possible to make your date periods any smaller than those represented by the categories. For example, if your date categories are months of the year, then it is not possible to make date values that let you aggregate your data weekly.

Approach is to use Q's function Q.EncodeDate(y, m, d) to assign date values for each category based on the year (y), month (m), and day (d) for each category. The date value should usually fall in the middle of the period. For example, if encoding months, you would use encode the 15th of the month, Q.EncodeDate(2018, 1, 15).

Once you have create the variable, change its Variable Type to Date.


In this example we assume we have date categories as shown in the screenshot below.


var _mydate = NaN;
if (d4 == 1) _mydate = Q.EncodeDate(2018, 1, 15);
else if (d4 == 2) _mydate = Q.EncodeDate(2018, 2, 15);
else if (d4 == 3) _mydate = Q.EncodeDate(2018, 3, 15);
else if (d4 == 4) _mydate = Q.EncodeDate(2018, 4, 15);
else if (d4 == 5) _mydate = Q.EncodeDate(2018, 5, 15);
else if (d4 == 6) _mydate = Q.EncodeDate(2018, 6, 15);
else if (d4 == 7) _mydate = Q.EncodeDate(2018, 7, 15);