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
*1. Create test data.
data list free/date(f8).
begin data
20111030
end data.
*2. Convert into date format.
compute realdate = date.dmy(mod(date,10**2),tru(mod(date,10**4)/10**2),tru(date/10**4)).
format realdate(edate10).
exe.
data list free/date(f8).
begin data
20111030
end data.
*2. Convert into date format.
compute realdate = date.dmy(mod(date,10**2),tru(mod(date,10**4)/10**2),tru(date/10**4)).
format realdate(edate10).
exe.
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.DMY
calculates this number. - It will be displayed as a normal date if its
FORMAT
is set (for example) toEDATE10
. - 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
TRUNCATE
andMODULUS
. - Like so, new variables holding the day, month and year can be created and these can be used in the
DATE.DMY
function. - Alternatively, the syntax extracting day, month and year can be placed directly into
DATE.DMY
as is done here.