SPSS tutorials website header logo SPSS TUTORIALS VIDEO COURSE BASICS ANOVA REGRESSION FACTOR

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.