SPSS Date Calculations - Simple Tutorial with Examples
SPSS tutorials website header logo SPSS TUTORIALS VIDEO COURSE BASICS ANOVA REGRESSION FACTOR

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 Date Variable in Data View

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.

FunctionUseExampleReturns
DATEDIFFCompute difference between two dates in given time unitdatediff(date1,date2,'days')Standard numeric value
DATESUMAdd / subtract number of given time units to date variabledatesum(date,10,'days')Date value
XDATEExtract date component from date variablexdate.month(date)Standard numeric value
DATE.DMYCreate date value from day, month, yeardate.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.

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

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

SPSS Datediff Function Example

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.

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

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 Datesum Function Example

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.

*1. Extract year from date.

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

SPSS - Extract Month from Date Syntax and Screenshot

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

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.

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

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

SPSS Date.dmy Function Example

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

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

*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.
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.dmy Function Example Flagging visits that started on or after February 20, 2014.

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.

*1. Compute holiday variable holding just zeroes.

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 Computes Wrong Week Numbers?

Wrong Week Numbers - Quick Demo

While working on data holding a record for each day, I wanted to create some graphs on week level. So I extracted the weeks with XDATE.WEEK but the week numbers returned by SPSS are nonsensical: every week starts on January 1 and most years end up with week 53 holding just 1 day.

SPSS Week Numbers Wrong

There's different standards for week numbers but I think the very definition of a week is a 7 day time span. The following syntax demonstrates the problem.

SPSS Week Numbers Syntax Example

*Create 500 empty cases.

input program.
loop mydate = 1 to 500.
end case.
end loop.
end file.
end input program.
execute.

*Convert mydate into actual date.

compute mydate = datesum(date.dmy(1,1,2013),mydate - 1,'days').
formats mydate (date11).

*Extract week and year from mydate.

compute week = xdate.week(mydate).
execute.

The result in data view may look normal at first. However, when we scroll down to case 365, we see that week 53 consists of 1 day. Like so, SPSS’ week numbers don't correspond to any conventional standard and can neither be converted into one.

ISO weeks in GoogleDocs

Interestingly, Google sheets has the isoweeknum function returning the ISO weeks I'm looking for. So a “workable solution” seemed to copy-paste these into an SPSS data file. Finally, MATCH FILES by date seemed to do the trick. And then I realized...
In the ISO week system, dates around new year’s can fall into a week from a different year. And unfortunately, GoogleDocs does not provide the years to which weeks belong. The screenshot below attempts to illustrate the problem.

SPSS Week Numbers Googledocs Years

Right. So extracting the year from December 30, 2013 obviously returns 2013. However, it falls in week 1, 2014. And neither SPSS nor GoogleDocs offers a function that'll insert 2014 into my dataset for this date.

Solution

Perhaps a bit of an anti climax but... no solution so far. I could go and look for a huge table holding a long date range and all ISO weeks plus the years in which they fall. And convert it to SPSS. And merge it into several data files. But I'd much rather avoid such an ugly solution.
So... any suggestions anybody? Please drop me a comment below if you've a better idea.

Thanks for reading!

Convert String Date to SPSS Date Variable

SPSS String to Date Variable Conversion Example

“I've a string variable in my data holding dates formatted as ‘01JAN2016’ without separators between the day, month and year components. To make things worse, the month abbreviations are in Dutch and 3 of those differ from their English counterparts. How can I change this string into an SPSS date variable?”

Step 1: Add Separators

The data are in stringdates.sav. Now, generally, ALTER TYPE is the way to go for this job but the day-month-year separators missing poses a problem here. The solution is to add those by combining CONCAT with CHAR.SUBSTR . We'll do so in a new string variable because this allows us to inspect if the conversion succeeded.

Syntax 1

*Create new date variable as string.

string newdate(a11).

*Add day, month, year from old string to new string and separate these components with dashes.

compute newdate = concat(
char.substr(mydate,1,2), /*start at character 1, extract 2 characters (day)
'-',
char.substr(mydate,3,3), /*start at character 3, extract 3 characters (month)
'-',
char.substr(mydate,6,4) /*start at character 6, extract 4 characters (year)
).

*Check if result is as desired.

execute.

*Convert stringdate with dashes to SPSS date variable.

alter type newdate (date11).

Step 2: Inspect Results

A huge flaw in ALTER TYPE is that it may result in system missing values without throwing any error or warning if it can't convert one or more values. Failing to detect this -not unlikely in larger datasets- may result in severely biased results. We'll therefore flag cases -if any- which have a system missing value on our new date variable but not an empty string value on our input variable.

Syntax 2

*ALTER TYPE may result in system missing values without error or warning so check if that's the case here.

compute flag = (missing(newdate) & mydate <> '').

*Move flagged cases -if any- to top of dataset.

sort cases by flag(d).

Result

SPSS ALTER TYPE conversion failures

Step 3: Replace Some Month Abbreviations

Note that we flagged some conversion failures but one case with an empty string value on our input variable is not one of them. The Dutch month abbreviations (such as “MRT” instead of “MAR”) are the reason for this. Fortunately, only 3 month abbreviations differ between English and Dutch.
We'll now convert our outcome variable back to string and recompute it. After doing so, we'll REPLACE the three deviant abbreviations in a DO REPEAT loop. After doing just that, we'll successfully convert our new variable into a date variable.

Syntax 3

*Delete flag.

delete variables flag.

*Change newdate back to string.

alter type newdate(a11).

*Recompute newdate as previously.

compute newdate = concat(
char.substr(mydate,1,2), /*start at character 1, extract 2 characters (day)
'-',
char.substr(mydate,3,3), /*start at character 3, extract 3 characters (month)
'-',
char.substr(mydate,6,4) /*start at character 6, extract 4 characters (year)
).

*Replace 3 Dutch abbreviations with their English counterparts.

do repeat #old = 'MRT' 'MEI' 'OKT' /#new = 'MAR' 'MAY' 'OCT'.
compute newdate = replace(newdate,#old,#new).
end repeat.

*Check if result is as desired.

execute.

*Convert to SPSS date variable - second attempt.

alter type newdate(date11).

Result

SPSS REPLACE Function Example

As we readily see, our conversion has now fully succeeded. In a larger dataset, you might want to inspect the result more carefully by flagging conversion failures like we did previously.

We really enjoyed writing this little tutorial. It nicely shows how combining the right building blocks gets a seemingly complicated job done with minimal effort and perfect precision. Hope you liked it too!

SPSS Time Variables Tutorial

Having a solid understanding of what SPSS time variables are, you may find calculations on them surprisingly easy. This tutorial will demonstrate SPSS' main time functions. However, we'll also show that we often don't even need them for getting things done.
Throughout this tutorial, keep in mind that SPSS time variables contain time spans in numbers of seconds that may or may not express clock times. Second, time variables are numeric variables so all numeric functions can be applied to them.
We encourage you try the time calculations we'll demonstrate yourself. You can do so by downloading and opening clock_card.sav.

SPSS Time Variables in Data View

SPSS Main Time Functions

Most of SPSS' date functions are intended for time variables as well. After outlining them in the table below, we'll take a closer look at them in the remainder of this tutorial.

FunctionUseExampleReturns
DATEDIFFCompute difference between two times in given time unitDATEDIFF(time1,time2,'minutes')Standard numeric value
DATESUMAdd number of given time units to time variableDATESUM(time,8,'hours')Time value
XDATEExtract time component from time variableXDATE.HOURS(time)Standard numeric value
TIME.HMSCreate time value from hours, minutes, secondsTIME.HMS(20,15,30)Time value

SPSS DATEDIFF Function

Our data contain the entry and exit times of an employee as registered with a clock card. We first want to know how much time he spent in the office per day. The syntax below shows how to do so with and without using DATEDIFF. The screenshots show the results of both options.

SPSS Time Difference Example

SPSS DATEDIFF Syntax Example

*1. Compute duration in seconds.

compute duration_time = exit - entry.
exe.

*2. Display duration in seconds as time.

formats duration_time(time8).

*2. Compute duration in minutes with DATEDIFF.

compute duration_minutes = datediff(exit,entry,'minutes').
exe.

*4. Hide decimals.

formats duration_minutes(f3).
SPSS Datediff Example

SPSS DATESUM Function

Employees are supposed to spend 8 hours per day in the office. That is, their entry times should be their exit times minus 8 hours. Such time subtractions (or additions) are easily accomplished by using DATESUM. However, realizing that hours consist of 3600 seconds, we may obtain the same result with an ordinary addition as shown in the second example.

SPSS Datesum Example

SPSS DATESUM Syntax Example

*1. Compute entry_target (8 hours before leaving) in seconds.

compute entry_target = datesum(exit,-8,'hours').
exe.

*2. Display entry_target as time.

formats entry_target(time8).

*3. Alternative to datesum for exit_target (8 hours after entry).

compute exit_target = entry + 3600 * 8.
exe.

*4. Display exit_target as time.

formats exit_target(time8).

SPSS XDATE Function

Employees are supposed to be in before 10 AM. One way to flag late entries is to extract the hours from the entry times with XDATE. XDATE needs to be suffixed with the time unit we wish to extract as in XDATE.HOURS. Finally, we'll RECODE the hours into our flag variable.

SPSS Xdate Example

SPSS XDATE Syntax Example

*1. Extract hours from entry.

compute entry_hours = xdate.hours(entry).
exe.

*2. Flag cases where entry_hours >= 10 (late entry).

recode entry_hours(lo thru 9 = 0)(10 thru hi = 1) into late_entry.
exe.

SPSS TIME.HMS Function

SPSS time variables hold numbers of seconds. TIME.HMS converts a number of hours, minutes and seconds into seconds and is thus creates SPSS time values from normal time components.
The minutes and seconds are optional; if omitted, they'll default to zero. That is, TIME.HMS(10) is a shorthand for TIME.HMS(10,0,0) and returns 36,000 (seconds). We can show this value as 10:00:00 by setting its format to TIME8.
The syntax below uses TIME.HMS as an alternative way to flag late entries.

SPSS Time.Hms Example

SPSS TIME.HMS Syntax Example

*1. Compute entry_cutoff as 10 AM.

compute entry_cutoff = time.hms(10,0,0).
exe.

*2. Display entry_cutoff as time.

formats entry_cutoff(time8).

*3. Delete late_entry before recalculating it.

delete variables late_entry.

*4. Recalculate late_entry.

if entry < entry_cutoff late_entry = 0.
if entry >= entry_cutoff late_entry = 1.
exe.

SPSS Time Comparisons

SPSS time comparisons are utterly simple when we realize that SPSS time values are just numbers of seconds that are shown as hours, minutes and seconds. For comparing an SPSS time value to a normal time value (hours, minutes and seconds), simply convert the latter into seconds. TIME.HMS does just that. Next, simply use SPSS' standard operators such as >, <= and others.
For example, employees are not supposed to leave before 4 PM. The syntax below shows a super shorthand for flagging early exits.The unusual COMPUTE command is explained in Compute A = B = C.

SPSS Time Comparison Example

SPSS Time Comparison Syntax Example 1

*Super shorthand for flagging early exits (before 4 PM).

compute early_exit = exit < time.hms(16).
exe.

SPSS Time Comparison Example 2

Because TIME.HMS is a function, it can be substituted in other functions, particularly RANGE. The following example shows how to use it for flagging entries during rush hours (from 8 until 9 AM).

*Flag entry times between 8 and 9 with RANGE.

compute rush_hour_entry = range(entry,time.hms(8),time.hms(9)).
exe.
SPSS Time Comparison Example

SPSS Time Variables in AGGREGATE

This final example again reemphasizes that SPSS time variables are numeric variables, holding seconds, on which normal numeric functions can be used.
For example, employees are supposed to work 40 hours per week. To what extent do our data meet that criterion? We already calculated duration_time, which is a time variable holding seconds. We can simply sum it per week by using AGGREGATE. This results in seconds per week which we'll show as normal times by setting their format to TIME8.

SPSS Time Variable in Aggregate
*1. Compute seconds in the office per week.

aggregate outfile *
/break week
/week_hours = sum(duration_time).

*2. Show seconds as hours, minutes, seconds.

formats week_hours(time8).

SPSS Datetime Variables Basics

Working with SPSS datetime variables is not hard at all if you understand some basics. This tutorial walks you through just those. Those who'd like to follow along may download and open hospital.sav.

SPSS Datetime Variable in Data View

SPSS Datetime Variables - What Are They?

SPSS datetime variables are variables that hold the numbers of seconds between the year 1582 and a given time on a given date. SPSS datetime values may look complicated (containing letters of months and dashes) but their values are really nothing more than huge numbers. The actual values are shown by specifying an f format, the syntax for which is formats exit_moment(f1).

SPSS Datetime Values in F Format

Note that this doesn't change the values in any way; they're merely displayed differently. Don't let their unusual appearance fool you: SPSS datetime variables are numeric variables. This implies that all standard numeric functions can be used on them. However, for calculations on SPSS datetime variables we'll mostly use SPSS date functions.

SPSS Datetime Formats

We just saw that SPSS datetime values really are huge numbers of seconds. For displaying them as normal dates with times, set their format to one of the two formats outlined below.

Variable TypeFormat familyFormat (example)Shown as
NumericDatetimeDatetime178-Jan-2013 18:34
NumericDatetimeDatetime208-Jan-2013 18:34:05

Date, Time and Datetime

The relation between SPSS date variables, time variables and datetime variables can be seen from a quick comparison of their definitions:

We conclude from this that SPSS date values can be seen as datetime values with a 00:00:00 time component. Running formats entry_date(datetime20). confirms this; SPSS date values can be properly displayed as datetime20 because their actual values are very similar to datetime values.

SPSS Date Values in Datetime Format

Reversely, datetime values can be displayed as dates as well. If doing so, just keep in mind that the time component does not disappear by no longer displaying it.

SPSS Datetime from Date and Time

At this point we may see that SPSS datetime values are simply the sum of a date value and a time value. Running the syntax below confirms this.

*1. Date (seconds) + time (seconds) = datetime (seconds).

compute entry_moment = entry_date + entry_time.
exe.

*2. Show seconds as normal date with time.

formats entry_moment(datetime20).
SPSS Combine Date and Time into Datetime Combining date and time into datetime - it really that simple.

SPSS Extract Date from Datetime

SPSS users who understand datetime variables will rarely -if ever- want to extract their date components. For the sake of completeness, the official way is to create the date variable using DATE.DMY. We obtain the required day, month and year components by applying XDATE to the datetime variable.
Note the day of the month is captured by XDATE.MDAY; XDATE.DAY is not valid in SPSS.In the syntax below, we first create day, month and year as intermediate variables before combining them with DATE.DMY. This step may be skipped by substituting XDATE into DATE.DMY which we'll demonstrate when extracting time values from datetime values.

*1. Extract date, month, and year into new variables.

compute day = xdate.mday(exit_moment).
compute month = xdate.month(exit_moment).
compute year = xdate.year(exit_moment).
exe.

*2. Compute date from (extracted) day, month and year components.

compute exit_date = date.dmy(day,month,year).
exe.

*3. Display as normal date values.

formats exit_date(date11).

The unofficial way to extract date values from datetime values uses SPSS TRUNC function; we remove the time portion from datetime values by rounding them down to 86400 seconds (one day).

*1. Delete exit_date before recomputing it.

delete variables exit_date.

*2. Extract date from datetime by TRUNC function.

compute exit_date = trunc(exit_moment,86400).
exe.

*3. Show date in date format.

formats exit_date(date11).

SPSS Extract Time from Datetime

SPSS time values can be created from hours, minutes and seconds by TIME.HMS. Again, these components can be extracted from datetime values by using XDATE as shown in the syntax below.

*1. Compute time values by combining hour, minute, second components extracted from datetime values.

compute exit_time = time.hms(xdate.hour(exit_moment),xdate.minute(exit_moment),xdate.second(exit_moment)).
exe.

*2. Show seconds as normal times.

formats exit_time(time8).

A faster alternative here is using SPSS MOD function; we basically throw away the date component by removing all 86400-folds (a day has 86400 seconds) from the datetime values.

*1. Remove date component from datetime values.

compute exit_time = mod(exit_moment,86400).
exe.

*2. Show seconds as normal times.

formats exit_time(time8).

SPSS Datetime Variables Tutorial

This tutorial shows how to work proficiently with SPSS datetime variables. You can follow along with it by downloading and opening hospital.sav.

SPSS Datetime Variable in Data View

SPSS Main Datetime Functions

This tutorial will cover the datetime functions outlined in the table below. Most of them apply to SPSS date variables and time variables as well because their values are stored in numbers of seconds too.

FunctionUseExampleReturns
DATESUMAdd number of given time units to datetime variableDATESUM(datetime,1,'months')Time value
DATEDIFFCompute difference between two datetime variables in given time unitDATEDIFF(datetime1,datetime2,'hours')Standard numeric value
CTIMEConvert seconds to other time unit without truncationCTIME.DAYS(timespan)Standard numeric value
XDATEExtract date or time component from datetime variableXDATE.HOURS(time)Standard numeric value
DATE.DMYCreate date value from day, month and yearDATE.DMY(10,2,2015)Date value
TIME.HMSCreate time value from hours, minutes, secondsTIME.HMS(20,15,30)Time value

SPSS Date and Time to Datetime

We'll first combine entry_date and entry_time into entry_moment because we'll need it a bit later on. The syntax below shows how to do so with a very basic COMPUTE command followed by FORMATS.

*1. Combine date and time into datetime (in seconds).

compute entry_moment = entry_date + entry_time.
exe.

*2. Display seconds as normal date with time.

formats entry_moment(datetime20).

SPSS DATESUM Function

SPSS DATESUM adds to datetime variables a given number of time units (days, hours and so on). Specify a negative number of time units for subtraction. For example, the hospital staff wants to contact their patients for a survey exactly 1 month after they've left the hospital. A notification should be sent 7 days prior to contacting patients.
Both datetime variables are easily created with SPSS DATESUM as shown in the syntax below. The result in data view is shown in the following screenshot.

*1. Compute contact_date = 1 month after leaving hospital.

compute contact_date = datesum(exit_moment,1,'months').
exe.

*2. Show contact_date as normal dates with times.

formats contact_date(datetime20).

*3. Compute notify_date as 7 days prior to contact_date.

compute notify_date = datesum(contact_date,-7,'days').
exe.

*4. Show notify_date as normal dates with times.

formats notify_date (datetime20).
SPSS Datetime Datesum Example

SPSS DATEDIFF Function

SPSS DATEDIFF function returns the difference between two datetime values in a given time unit (hours, days and so on). For example, how long did the patients stay in the hospital? The syntax below first answers the question by using DATEDIFF.
Now, keep in mind that DATEDIFF truncates (rounds down) its return values. Personally, we prefer using a basic subtraction here. Because datetime values are numbers of seconds, the result is a number of seconds too. However, we can easily show these as hours, minutes and seconds by giving it a time format. This is shown in the second example below.

SPSS DATEDIFF Syntax Example

*1. Compute duration in days with DATEDIFF.

compute duration_days = datediff(exit_moment,entry_moment,'days').
exe.

*2. Compute duration in seconds by basic subtraction.

compute duration_time = exit_moment - entry_moment.
exe.

*3. Show duration_time in hours, minutes, seconds.

formats duration_time(time8).
SPSS Datetime Compute Duration

SPSS CTIME Function

SPSS CTIME converts seconds to other time units such as hours, days or months.Oddly, CTIME.YEARS is painfully missing in SPSS while CTIME.SECONDS -which does basically nothing- is present instead. In contrast to DATEDIFF, return values aren't truncated.
Note that duration_time is a time variable so it really holds numbers of seconds. We can convert those to the desired time units with CTIME as demonstrated below; the syntax recalculates duration in days but this time without truncating the outcome values.
An alternative to CTIME here is using a basic division; since a day holds 86,400 seconds, dividing time values by 86400 is equivalent to using CTIME.DAYS. This is shown in the second example below.

SPSS CTIME Syntax Example

*1. Convert duration_time (seconds) to duration_day.

compute duration_days = ctime.days(duration_time).
exe.

*2. Alternative second to day conversion.

compute duration_days = duration_time / 86400.
exe.
SPSS CTIME on Datetime Example

SPSS XDATE Function

SPSS XDATE extracts date components from date, time and datetime values. The syntax below thus shows how to extract the day, month and year from exit_moment. Note that XDATE.MDAY rather than XDATE.DAY returns the day of the month.

*Extract day, month and year from datetime with XDATE.

compute exit_day = xdate.mday(exit_moment).
compute exit_month = xdate.month(exit_moment).
compute exit_year = xdate.year(exit_moment).
exe.

SPSS Datetime Comparisons

Comparing two SPSS datetime variables is straightforward and can be done with the usual operators such as >, <= and others. For example, exit_moment must obviously be greater (later in time) than entry_moment for all visits. The syntax confirms that this holds for all visits by using IF.

SPSS Datetime Comparison Example 1

*1. Compute new variable holding just zeroes.

compute exit_after_entry = 0.

*2. Compare datetimes to detect abnormal cases.

if exit_moment > entry_moment exit_after_entry = 1.

*3. All cases flagged, no abnormalities.

frequencies exit_after_entry.

As we mentioned before, datetime values are numbers of seconds. You can compare datetime values to a given date by converting the latter into seconds as well. This is readily done by DATE.DMY as we'll show in a minute.
For example, a report criticizing the hospital was published on November 2nd., 2014. As a first step in evaluating its impact on patient ratings, we'll flag all visits that ended on or after this date.The COMPUTE command used in the syntax below looks a bit weird. It is explained in Compute A = B = C. Keep in mind that date values are identical to datetime values with 00:00:00 as their time components.

SPSS Datetime Comparison Example 2

*Flag all cases whose exit_moment is during or after November 2nd., 2014.

compute after_report = exit_moment > date.dmy(2,11,2014).
exe.
SPSS Datetime Comparison Example

The report was published at 12:10:05 (10 minutes, 5 seconds past noon). Note that two visits ending on the publication date but before the publication time were flagged. How can we exclude such cases?
Well, remember that an SPSS datetime value is identical to the sum of an SPSS date value and an SPSS time value. The syntax below uses this in order to compare a datetime variable to a given date and time.

SPSS Datetime Comparison Example 3

*Flag cases whose exit was later than November 2nd., 12:10:05 in the afternoon.

compute after_report = exit_moment > date.dmy(2,11,2014) + time.hms(12,10,5).
exe.

SPSS – Convert String into Date Variable

For converting string variables to date variables, ALTER TYPE is the way to go. This tutorial demonstrates how to do this and points out a couple of caveats. For those who can't use ALTER TYPE, we'll propose some alternatives.

SPSS Alter Type

The syntax below first creates a mini dataset holding 8 string variables. They demonstrate the 8 most common date formats. Next, each string variable is converted to a date variable by using ALTER TYPE.

SPSS String to Date Syntax

*1. Create mini dataset.

data list free/d1 to d8(8a20).
begin data
31-dec-99 31.dec.1999 12/31/99 12-31-1999 31.12.99 31/12/1999 99-12-31 1999.12.31
end data.

*2. Convert all strings to dates.

alter type d1(date9).
alter type d2(date11).
alter type d3(adate8).
alter type d4(adate10).
alter type d5(edate8).
alter type d6(edate10).
alter type d7(sdate8).
alter type d8(sdate10).

String to Date Syntax Notes

SPSS String to Date without Alter Type

Note that ALTER TYPE can only be used for a limited number of date formats. Some more exotic formats may require a more flexible approach. Second, those on SPSS versions 15 and below don't have ALTER TYPE since it was introduced in version 16.

For both scenarios, we'll usually extract the year, month and day by using SUBSTR, often combined with INDEX and RINDEX. We'll then convert these into an SPSS date variable by using the DATE.DMY function. Finally, we'll display the number of seconds it holds as a more readable date by using FORMATS.

SPSS String to Date Syntax

*1. Create mini dataset.

data list free/s1 s2(2a20).
begin data
1.1.1999 1-jan-99 2.28.1999 2-feb-99 3.31.1999 3-mar-99 4.30.1999 4-apr-99 5.31.1999 5-may-99 6.30.1999 6-jun-99
7.31.1999 7-jul-99 8.31.1999 8-aug-99 9.30.1999 9-sep-99 10.31.1999 10-oct-99 11.30.1999 11-nov-99 12.31.1999 12-dec-99
end data.

*2. Extract day, month and year from string.

compute day = number(char.substr(s1,char.index(s1,'.') + 1,char.rindex(s1,'.') - char.index(s1,'.')),f2.0).
compute month = number(char.substr(s1,1,char.index(s1,'.') - 1),f2.0).
compute year = number(char.substr(s1,char.rindex(s1,'.') +1),f4.0).
exe.

*3. Compute date variable.

compute d1 = date.dmy(day,month,year).
exe.

*4. Display as date.

formats d1(adate10).

Dealing with Months as Letters

The previous example converted the first string variable but the second is slightly harder. This is because DATE.DMY requires three numbers but months are now shown as letters (e.g. JAN, FEB and so on). An easy way to fix this is to REPLACE the months by numbers (00 through 12) using DO REPEAT as shown below. After doing so, one can proceed as in the previous example.

*Replace month letters by month numbers in string.

do repeat s = 'jan' 'feb' 'mar' 'apr' 'may' 'jun' 'jul' 'aug' 'sep' 'oct' 'nov' 'dec' / n = 1 to 12.
compute s2 = replace(s2,s,string(n,n2)).
end repeat.
exe.

Extract a Year from a Date

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

Notes

SPSS Time Variables Basics

Introduction

Working efficiently with SPSS time variables is not hard if you understand some basics. This tutorial walks you through just those. We recommend you follow along by downloading and opening hospital.sav.

SPSS Time Variable in Data

SPSS Time Variables - What Are They?

SPSS time variables are variables that hold time intervals in numbers of seconds. Although the actual time values are just simple numbers, they are usually displayed as hours, minutes and seconds. Don't let this appearance confuse you; it's the underlying numbers of seconds that time calculations act upon.
Note that SPSS time variables are numeric variables: all of SPSS' numeric functions can be used on them in the exact same way as you would on other numeric variables. However, we often use SPSS time functions for calculations on time variables.

SPSS Common Time Formats

Time values are numbers of seconds but we usually display them as hours, minutes and seconds. We can do so by setting their format to one of SPSS' time formats. The table below shows the most common ones.

Variable TypeFormat familyFormat (example)Shown as
NumericTimeTime516:56
NumericTimeTime816:56:10

SPSS Time Variables - Example

We'll now focus on entry_time in our data. We'll take a look at its actual values (numbers of seconds) in data view; running the following line of syntax will show them: formats entry_time(f1).

SPSS Time Variable Actual Values

Note that this doesn't change the values in any way; they're merely displayed differently. We can show them as normal time values again by running formats entry_time(time8). Knowing that time values are really just numbers of seconds makes it pretty easy to work with them. For instance, we can set all time values one hour ahead by simply adding 3600 (seconds in one hour) to them by running the syntax below.

SPSS Time Variable Syntax Example

*1. Add 3600 seconds (= 1 hour) to all time values.

compute entry_time = entry_time + 3600.

*2. Sort cases on entry_time descendingly.

sort cases by entry_time(d).

Looking at the result, we notice something strange: the first couple of values exceed 24 hours. That is pretty odd in this particular case. However, it generally makes sense when we understand the nature of SPSS time values, which we'll discuss next.

SPSS Time Variable Large Values

SPSS Time Values

Keep in mind that SPSS time values contain time intervals. These often indicate clock times as intervals between midnight and a given time point. However, this is not necessarily the case.
For example, the duration of a hotel stay (difference between exit moment and arrival moment) will typically be entered as a time variable in SPSS. This time interval probably doesn't start at midnight and will often exceed 24 hours.
In short, SPSS time variables hold time spans that may or may not indicate clock times. SPSS time values may also exceed 24 hours because they don't contain a day or month component.

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 Variable in Data View

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 TypeFormat familyFormat (example)Shown as
NumericDateDate1119-jan-2013
NumericEdate (= European date)Edate1019.01.2013
NumericAdate (= American date)Adate1001/19/2013
NumericSdate (= Sortable date)Sdate102013/01/19

As suggested by this table, we recommend that you always display years as 4 digits instead of 2.

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 f format. Running the following line of syntax does just that. formats entry_date(f1).

SPSS Date Variabe in F Format

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.

*Add one day (= 86400 seconds) to date variable.

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.