Introduction
Extracting the year (or any component) from a date is straightforward with the XDATE (“EXtract DATE“) function. Copy-paste-run the syntax presented below for a demonstration of XDATE.
SPSS Xdate Syntax Example
*1. Create mini test data.
data list free/date(edate10).
begin data
’01/01/01′
’12/12/12′
end data.
data list free/date(edate10).
begin data
’01/01/01′
’12/12/12′
end data.
*2. Extract the year.
compute year_from_date = xdate.year(date).
exe.
*3. Hide decimals for year_from_date.
format year_from_date(f4.0).
Notes
- The
XDATEfunction can also be used for extracting the day or month from a date or the date portion from aDATETIMEvariable. - The only thing to make sure is that the input variable is a proper date variable.
