 SPSS TUTORIALS

# 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 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 numer 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 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 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 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 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 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 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 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. *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).

# Tell us what you think!

*Required field. Your comment will show up after approval from a moderator.

# THIS TUTORIAL HAS 12 COMMENTS:

• ### By Maaike on August 21st, 2016

Hello Ruben, Despite your clear explanations, I am stuck with one of my calculations. As a sleep researcher, many of my patients go to bed at varying times, e.g. between 22:00 and 04:00. Calculating an average keeps leading to problems because SPSS does not seem to recognise the 24:00 midpoint. The average would be 13:00, where in reality it is 01:00. Any suggestions how to solve this ?

• ### By Ruben Geert van den Berg on August 21st, 2016

Hi Maaike! The problem you describe is simply inherent to all time variables (I have the exact same problem in GoogleSheets but I wouldn't recommend my super ugly workaround to anyone).

The solution is to use DATETIME instead of TIME variables. Since both dates and times in SPSS are expressed in seconds, you can simply add them:

COMPUTE MOMENT = SOMEDATE + SOMETIME.

and set the format of the result to DATETIME22

FORMATS MOMENT(DATETIME22).

Time differences between DATETIME variables -again- are as simple as basic subtractions but the result will be in seconds so divide it by (60 * 60 =) 3600 for hours.

I hope you're able to resolve the issue now but if not, please let me know because I'll be more than happy to take a closer look at it ok?

P.s. you're a Dutch sleep researcher? I took some classes from Gerard Kerkhof (long ago). Perhaps you're familiar with him or his work -although he may have retired by now.

• ### By Caner MERT on November 1st, 2016

How I can collect time together. Time is hour and minutes. For example 270:00 I want to collect 120:00 minutes 270:00(+)120:00 = 390:00 minutes.

Thank you very much

• ### By Ruben Geert van den Berg on November 1st, 2016

Hi Caner! 270:00 is not an SPSS time format. In SPSS, it would be shown as 04:30:00 (4 hours and 30 minutes). That being said, time in SPSS is in seconds so divide it by 60 for computing minutes. Just use a basic SPSS addition for adding minutes to that. I'll add a tiny syntax example below. Hope it makes sense!

data list free/entry (time8).
begin data
01:23:45
end data.

*Since time is in seconds, compute minutes by dividing by 60.
compute minutes = entry / 60.
execute.

compute minutes = minutes + 15.
execute.

*Convert back to time variable.
compute exit = minutes * 60.
execute.

formats exit (time8).

• ### By Jos Kox on May 1st, 2017

Hi there, I'm just a beginner in SPSS. I have hour and minute variables, that I want to change to a total of minutes. How can I do this with a syntax. For example the hour variable is 1 (meaning 1 hour), the minute variable is 10 (meaning 10 minutes). I have to convert the 1 to 60 and add up the 10 to get a total of 70 minutes. How do I do that through a syntax in SPSS?