Date function examples
Using a date function to determine the day of the week of a date
This example shows the use of the date function weekday. You define a Derived Single Response variable to work out the day of the week for a particular date.
- Click to display the Variables window.
- Click to add a new variable.
- Specify the Variable Details:
- Name: V6
- Label: Day of the week restaurant visited
- Type: Derived (the variable will derive its data from other existing variables).
- Response: Single (each respondent will fall into only one of the new codes, as there is only one date of visit per case).
- Specify the Code Details:
- Click to save the variable. The variable can then be used in tables and charts in the usual way.
Calculating age from a date of birth
These instructions tell you how to create variables to calculate the respondent’s age from their date of birth and then put the respondents into age bands, based on the difference between a date of birth variable and the date function today. The instructions also apply if you have another date question in the questionnaire, which you want to use instead of today.
For this example, the date of birth variable is Q1a. It assumes a four-digit year.
- Click to display the Variables window.
- Click to add a new variable.
- Specify the Variable Details as:
- Name: Age
- Label: Calculated age in years
- Type: Derived (the variable will derive its data from other existing variables).
- Response: Quantity (the response will be a numeric value for each case).
- Specify the Code Values to calculate the number of years. Note the use of \ to extract the whole number of years.
Code | Code Label | Value |
NA | Not Asked | Q1a Missing |
OK | Valid | (today-Q1a)\365.25 |
- Click to save the variable. You can then use this to analyse respondents’ ages.
- The Not Asked value is specified to ensure that the calculation is only performed if data exists for the date question.
- To band the respondents into age groups, create a new variable:
- Name: Agegroup
- Label: Age groups
- Type: Derived
- Response: Single (each respondent will only fall into one of the new codes).
- Specify the Code Details:
- The codes are assigned in order, so all responses that have not been sorted into previous bands appear as 65 plus.
- Click to save the variable.