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.
|DATEDIFF||Compute difference between two times in given time unit||DATEDIFF(time1,time2,'minutes')||Standard numeric value|
|DATESUM||Add number of given time units to time variable||DATESUM(time,8,'hours')||Time value|
|XDATE||Extract time component from time variable||XDATE.HOURS(time)||Standard numeric value|
|TIME.HMS||Create time value from hours, minutes, seconds||TIME.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
compute duration_time = exit - entry.
*2. Display duration in seconds as time.
*2. Compute duration in minutes with DATEDIFF.
compute duration_minutes = datediff(exit,entry,'minutes').
*4. Hide decimals.
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
compute entry_target = datesum(exit,-8,'hours').
*2. Display entry_target as time.
*3. Alternative to datesum for exit_target (8 hours after entry).
compute exit_target = entry + 3600 * 8.
*4. Display exit_target as time.
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
compute entry_hours = xdate.hours(entry).
*2. Flag cases where entry_hours >= 10 (late entry).
recode entry_hours(lo thru 9 = 0)(10 thru hi = 1) into late_entry.
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
compute entry_cutoff = time.hms(10,0,0).
*2. Display entry_cutoff as time.
*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.
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
compute early_exit = exit < time.hms(16).
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).
compute rush_hour_entry = range(entry,time.hms(8),time.hms(9)).
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.
aggregate outfile *
/week_hours = sum(duration_time).
*2. Show seconds as hours, minutes, seconds.