For time calculations (such as the number of days between two dates) proper date variables are needed. In some cases, the digits of a numeric variable may represent year, month and day. This tutorial shows how to convert this format into an actual date variable.
Convert Numbers to SPSS Date Variables
In some cases, dates are represented as numeric variables whose digits represent a year, month and day (often referred to as YYYY-MM-DD). This format is not suitable for any time based calculations which require an actual date variables. The solution for this is to convert the original variable into an actual date variable. This is demonstrated by the syntax below.
SPSS Date Function Syntax Example
data list free/date(f8).
*2. Convert into date format.
compute realdate = date.dmy(mod(date,10**2),tru(mod(date,10**4)/10**2),tru(date/10**4)).
SPSS Date Function Explanation
- In SPSS, a date variable holds the number of seconds since (roughly) 1582.
- For any (day, month, year) combination, the function
DATE.DMYcalculates this number.
- It will be displayed as a normal date if its
FORMATis set (for example) to
- For the example data, the day, month and year correspond to the last, middle and first digits of the number.
- These are extracted by combining
- Like so, new variables holding the day, month and year can be created and these can be used in the
- Alternatively, the syntax extracting day, month and year can be placed directly into
DATE.DMYas is done here.