SPSS Date Calculations – A Quick Tutorial
Introduction & Practice Data File
SPSS date calculations are pretty straightforward. Just make sure you understand a handful of basics and keep it clean and simple. This tutorial shows you how to do just that. We'll use hospital.sav -shown below- throughout.

SPSS Main Date Functions
The table below shows SPSS’ main date functions. We'll show how to use them on a couple of examples below.
Function | Use | Example | Returns |
---|---|---|---|
DATEDIFF | Compute difference between two dates in given time unit | datediff(date1,date2,'days') | Standard numeric value |
DATESUM | Add / subtract number of given time units to date variable | datesum(date,10,'days') | Date value |
XDATE | Extract date component from date variable | xdate.month(date) | Standard numeric value |
DATE.DMY | Create date value from day, month, year | date.dmy(19,3,2015) | Date value |
SPSS DATEDIFF Function
SPSS DATEDIFF returns the number of time units
(such as hours, days or years) between two date values.
For instance, how many days ago did our respondents enter the hospital? We'll answer that by subtracting entry_date from the current date. The syntax below does just that.
compute today = $time.
execute.
*2. Show current date in date format.
formats today(edate10).
*3. Compute
compute days_ago = datediff(today,entry_date,'days').
execute.
*4. Don't show any decimal places.
formats days_ago(f4).
Result

Simple as that. Just one thing to keep in mind is that DATEDIFF truncates (rounds down) its outcome values. So 2 years and 363 days are returned as 2 years and 0 days if years are chosen as the time unit.
If you don't want that, extract the number of days between 2 dates and divide them by 365.25 like we did in How to Compute Age in SPSS?
SPSS DATESUM Function
SPSS DATESUM adds a number of time units to a date variable.
For subtracting time, just enter a negative value.
So say I want to contact respondents 3 months after they entered the hospital. However, 7 days before contacting them, they should be sent a notification. The syntax below shows how to add both dates to our data.
compute contact_date = datesum(entry_date,3,'months').
execute.
*2. Show time values as dates.
formats contact_date(date11).
*3. Subtract 7 days from contact_date as notify_date.
compute notify_date = datesum(contact_date,-7,'days').
execute.
*4. Show time values as dates.
formats notify_date(date11).
Result

SPSS XDATE Function
SPSS XDATE extracts a date component
(such as an hour, day or year) from a date.
For example, the syntax below first extracts the year from entry_date and then the month.
compute year = xdate.year(entry_date).
execute.
*2. Extract month from date.
compute month = xdate.month(entry_date).
execute.
*3. Hide decimals.
formats year month(f4).
*4. Apply value labels to month.
value labels month 1 'January' 2 'February' 3 'March' 4 'April' 5 'May' 6 'June' 7 'July' 8 'August' 9 'September' 10 'October' 11 'November' 12 'December'.
Result

Again, it's as simple as that. An important warning, however, is that XDATE.WEEK returns nonsensical week numbers as discussed in SPSS Computes Wrong Week Numbers? Unfortunately, there's no easy way to extract the ISO week numbers that you probably want. We built an SPSS-Python tool for it but it somehow stopped working around SPSS version 24.
Another thing to keep in mind is that there's no such thing as XDATE.DAY. Instead, use
- XDATE.MDAY for the day of the month (1 through 31);
- XDATE.WKDAY for the day of the week (1 through 7 where 1 is Sunday, not Monday);
- XDATE.JDAY for the day of the year (1 through 366).
SPSS DATE.DMY Function
SPSS DATE.DMY creates a date from its components
such as day, month and year.
So say I want to know how many days before 20 January 2015 patients entered the hospital. I'll just create a new date variable (or -rather- constant) holding this date and subtract it from entry_date.
compute start_date = date.dmy(20,1,2015).
execute.
*2. Show start_date as date.
formats start_date (date11).
*3. Compute days before start of data analysis.
compute days_passed = datediff(start_date,entry_date,'days').
execute.
Result

Obviously, DMY means “day, month, year” so that's the order in which we'll enter these date components. Now, in first instance, DATE.DMY results in a variable holding huge numbers. These are the numbers of seconds between the year 1582 and my actual data as explained in SPSS Date Variables Basics.
These huge numbers can be shown as normal dates by setting the appropriate formats. For instance,
FORMATS today (DATE11).
shows a date as “3-Sep-2018”. This is what we recommend because it's unambiguous which date this is. Please
avoid showing dates like “01-02-03”
because this could mean
- 1-Feb-2003 if its format is EDATE8 (European date),
- 2-Jan-2003 if its format is ADATE8 (American date) and
- 3-Feb-2001 if its format is SDATE8 (sortable date).
SPSS Date Comparisons
When we compare two numbers, we can simply ask if one number is larger than the other. The exact same, simple logic holds for date comparisons. However, there's one caveat: although we see “normal dates”, the underlying values (numbers of seconds since the year 1582) are used in date comparisons.
This may sound daunting but the solution is simple: if we want to compare an SPSS date value with some comparison date, we simply
convert the comparison date into an actual SPSS date.
We just saw how to do so easily: fill in the date components into DATE.DMY. We'll demonstrate this with some examples.
Say the hospital got a new CEO on February 20, 2014. We want to know if visits before this date are rated the same as visits after this date. We'll now select visits on and after February 20, 2014.
SPSS Date Comparison Example I
compute ceo = 0.
*2. If entry date is at least February 20th., 2014, entry during new CEO.
if (entry_date >= date.dmy(20,2,2014)) ceo = 1.
execute.
*3. Hide unnecessary decimals.
formats ceo(f1).
*4. Add value labels to new variable.
value labels ceo 0 'entry_date during old CEO' 1 'entry_date during new CEO'.
Result

SPSS Date Comparison Syntax Example II
The Summer holidays in 2014 were from June 30, 2014 through August 24, 2014. How can we select visits during these holidays? The syntax below shows how to do so easily by using DATE.DMY within RANGE.
compute holidays_2014 = 0.
*2. Change 0 to 1 if visit started during holidays 2014.
if (range(entry_date,date.dmy(30,6,2014),date.dmy(24,8,2014))) holidays_2014 = 1.
execute.
*3. Hide decimals.
formats holidays_2014(f1).
That's basically it for the main DATE calculations in SPSS. We could come up with a million more examples but you'll probably figure them out yourself pretty easily. We hope.
Thanks for reading!
SPSS Date Variables Basics
SPSS date variables may seem a bit puzzling at first. However, getting things done fast and accurately with SPSS date variables is not hard at all if you understand some basics. This tutorial will walk you through. You can follow along by downloading and opening hospital.sav.

SPSS Date Variables - What Are They?
First of all, SPSS date variables are numeric variables; their actual values are just numbers. More precisely, the numbers they hold are the number of seconds between the year 1582 and the start (midnight) of a given date. Realizing that it's these underlying values that date calculations act upon renders SPSS date logic straightforward.
Second, these huge numbers are usually shown as normal dates by setting their format to one of SPSS' main date formats. We'll outline some of those in the table below.
SPSS Date Formats
As we just mentioned, SPSS date values are always numbers of seconds. You can choose several formats for displaying them as actual dates. Some main options are given in the table below.
Variable Type | Format family | Format (example) | Shown as |
---|---|---|---|
Numeric | Date | Date11 | 19-jan-2013 |
Numeric | Edate (= European date) | Edate10 | 19.01.2013 |
Numeric | Adate (= American date) | Adate10 | 01/19/2013 |
Numeric | Sdate (= Sortable date) | Sdate10 | 2013/01/19 |
As suggested by this table, we recommend that you always display years as 4 digits instead of 2.A reason for this is explained in Two Digit Year in String - Cautionary Note.
SPSS Date Variables - Example
The aforementioned points may sound rather abstract so let's demonstrate them on our data. It contains one proper date variable, entry_date. We'll take a look at its actual values by giving it an an f format. Running the following line of syntax does just that. formats entry_date(f1).

The date values now look very differently in data view. However, they are still the exact same values that we had a few seconds ago; they're just displayed differently. We'll now show them as dates by running formats entry_date(sdate10). After doing so, the actual date values look like date values again. Again, keep in mind that actual values still haven't changed in any way.
SPSS Date Calculations
So why bother about the huge numbers that underlie SPSS date values? The reason is that its these values that SPSS date functions act upon.
When we understand that date values are really numbers of seconds, it becomes very easy to add, say, one day to date values. Note that one day is 60 (seconds) * 60 (minutes) * 24 (hours) = 86400 seconds. Now watch what happens when we simply add 86400 to our date variable by running the syntax below.
compute entry_date = entry_date + 86400.
exe.
Indeed, exactly one day has been added to all values. However, adding a month is more complicated because different months hold different numbers of days.
For such cases SPSS has some specialized date functions that render such operations a piece of cake. The next tutorial will walk you through the most important ones.