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.
*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).
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
XDATE
function can also be used for extracting the day or month from a date or the date portion from aDATETIME
variable. - The only thing to make sure is that the input variable is a proper date variable.
THIS TUTORIAL HAS 3 COMMENTS:
By Linda Martell on July 15th, 2015
Good
By Mike Burke on April 19th, 2018
The tutorial is not as complete as it should be. More often than not, someone has an existing data set they are using. The fact that you use date as a variable and function/syntax is somewhat confusing in trying to match up the example to the task at hand. I suggest defining the terms used in the example.
By Ruben Geert van den Berg on April 20th, 2018
Hi Mike, thanks for your comment!
I totally agree: using
date
as a variable name was a terrible thing to do. I'm surprised SPSS even accepts it.Please forgive me. I wrote this (very old) tutorial when I was young and stupid. I'll try and update this -with downloadable data instead of
data list...
when I'll find the time, ok?