SPSS date calculations are much easier than they may seem at first. The first key to success is understanding what SPSS date variables really are. Next, choose (and possibly combine) the right date functions. This tutorial will walk you through the main ones.

We recommend you follow along by downloading and opening hospital.sav.

## SPSS Main Date Functions

The table below shows SPSS' main date functions. The remainder of this tutorial will discuss them in more detail and shows how to use them for answering real world questions regarding our data.

Function | Use | Example | Returns |
---|---|---|---|

DATEDIFF | Compute difference between two dates in given time unit | datediff(date1,date2,'days') | Standard numeric value |

DATESUM | Add / subtract numer 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 function returns the number of given time units between two date values**. Importantly, DATEDIFF truncates (rounds down) its outcome values. For example, 2 years and 363 days are returned as 2 years and 0 days if years are chosen as the time unit.

How many days ago did our respondents enter the hospital? We can answer this by using subtracting entry_date from the current date using DATEDIFF. We'll create the current date in our data by using $time. The syntax below shows how to do so.A faster alternative is simpy using $time in step 3 instead of adding it as a new variable to the data. The following screenshot shows the result in data view.

## SPSS DATEDIFF Syntax Example

***1. Create current date as new variable in data.**

compute today = $time.

exe.

***2. Show current date in date format.**

formats today(edate10).

***3. Compute**

compute days_ago = datediff(today,entry_date,'days').

exe.

***4. Hide empty decimals.**

formats days_ago(f4).

## SPSS DATESUM Function

**SPSS DATESUM adds or subtracts a number of given time units from a date variable**. Enter a positive value for addition or a negative values for subtraction. We'll illustrate both with an example on our data.

A researcher wants to contact respondents 3 months after they entered the hospital. However, 7 days prior to contacting them, they should be sent a notification for this. The syntax below shows how to do so. The result is shown in the screenshot that follows.

## SPSS DATESUM Syntax Examples

***1. Add 3 months to entry_date for calculating contact_date.**

compute contact_date = datesum(entry_date,3,'months').

exe.

***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').

exe.

***4. Show time values as dates.**

formats notify_date(date11).

## SPSS XDATE Function

**SPSS XDATE extracts a date component from a date value**. The component to extract is appended as a suffix to the function as in XDATE.YEAR or XDATE.MONTH. 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).

For example, the syntax below first extracts the year from entry_date and then the month. The following screenshot shows the result.

## SPSS XDATE Syntax Examples

***1. Extract year from date.**

compute year = xdate.year(entry_date).

exe.

***2. Extract month from date.**

compute month = xdate.month(entry_date).

exe.

***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'.

## SPSS DATE.DMY Function

**SPSS DATE.DMY Function creates a date value from its components.** That is, given a day, month and year, it calculates the number of seconds between the year 1582 and the date defined by these components.

The function's DMY suffix indicates the order in which the components are given: first day, then month, then year. The result can be shown as a normal date by a FORMATS command and can be used in all sorts of date calculations.

For example, a researcher starts analyzing these data on January 20, 2015. He wants to **calculate how many days before the start** of his data analysis patients entered the hospital.Note that he can't use the current date from $time here because he'd get different values every time he'd run his syntax. The syntax below shows how he can do so.

## SPSS DATE.DMY Syntax Example

***1. Add start_date as January 20th., 2015 as new variable to data.**

compute start_date = date.dmy(20,1,2015).

exe.

***2. Show start_date as date.**

formats start_date (edate10).

***3. Compute days before start of data analysis.**

compute days_passed = datediff(start_date,entry_date,'days').

exe.

## SPSS Date Comparisons

When we compare two numbers, we can simply ask whether 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 actual 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 SPSS date value** too. 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**.Note that the syntax can be shortened by combining the first two steps as explained in Compute A = B = C. Note the use of IF in step 2.

## SPSS Date Comparison Syntax Example 1

***1. Compute new_ceo variable (all zeroes).**

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.

exe.

***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'.

## SPSS Date Comparison Syntax Example 2

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 do so easily by using DATE.DMY within RANGE.

***1. Compute holiday variable holding just zeroes.**

compute holidays_2014 = 0.

***2. Insert 1 if visit started during holidays 2014.**

if range(entry_date,date.dmy(30,6,2014),date.dmy(24,8,2014)) holidays_2014 = 1.

exe.

***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 by now. Thanks for reading and feel free to leave us a comment below!

## This tutorial has 4 comments

## By Ruben Geert van den Berg on June 15th, 2017

Hi Joy!

At first glance, your syntax looks great. I can't troubleshoot it without seeing a sample of data, the result of your syntax and the desired result. If you could share a more tangible/replicable demonstration of the issue, that would help either me or anybody else who may assist.

Until then, perhaps run all examples in our LAG tutorial and see if one of those corresponds to what you're looking for.

## By Joy Bowles on June 15th, 2017

Thanks Ruben. I have a question that I cannot find a solution to anywhere on the internet, so I'm hoping you can please help me. I have a list of diabetes review test results and review dates for 2000 patients, with 65 different investigation results per review, per patient, and I want to create a variable that will number each all the test results from each review with a review number, sorted from oldest to newest (Review 1, Review 2 etc).

I tried with this set of commands, but could only get it to work for patient1, because I don't know how to make it pick the $CASENUM for the first test result for patient2:

IF $CASENUM = 1 Counter = 1.

EXECUTE.

IF dtTest = lag(dtTest) Counter = lag(Counter).

EXECUTE.

[repeat these next two steps until you reach the end of patient1 results]

IF lPatientID = 1 and dtTest ne lag(dtTest) Counter = lag(Counter) + 1.

EXECUTE.

IF dtTest = lag(dtTest) Counter = lag(Counter).

EXECUTE.

If you can't help me, could you please recommend someone who might be able to?

Kind regards,

Joy Bowles

## By Lauren on September 5th, 2016

Thank you! I really appreciated this. You are great.

## By kelly on April 14th, 2016

Thanks for sharing this!!!!! You are so knowledgeable and professional, and kind. I believe that there have been a lot of people benefit from your website!!!!!