Skip to main content

SPSS Tutorials: Date-Time Variables in SPSS

This tutorial covers how SPSS treats Date-Time variables, and how to use the Date & Time Wizard to create and compute variables using dates and times.

Working with Dates and Times in SPSS

Datasets often include variables that denote dates or time. Thus, it is important to know how SPSS treats and works with such variables. In the following sections, we will discuss:

  • Standard formats for dates and time.
  • Setting the century range for two-digit years.
  • Defining date-time variables in the Variable View window and through the Date and Time Wizard.
  • Converting a string variable containing a date into an actual date variable.
  • Computing the elapsed time between two date-time variables using the Date and Time Wizard.

Standard Formats for Dates and Times

SPSS can recognize dates and times that appear in a variety of formats. Below are the standard formats for dates and time. For each format, the letters (left column) represent the text or numbers for each date or time in the example (right column).

Thus, for dates, 

  • “d” = day of month (1, 2, ..., 31)
  • “m” = month
    • "mm" = month as two digit number (01, 02, ..., 12)
    • "mmm" = month as three character abbreviation (JAN, FEB, ..., DEC)
  • “y” = year
    • "yy" = two digit year (century is omitted)
    • "yyyy" = four digit year
  • “q” = quarter
  • "w" or “wk” = week of year (1, 2, ..., 53)
  • “h” = hour (1, 2, ... 24)
  • “m” = minute (1, 2, ..., 60)
  • “s” = seconds (1, 2, ..., 60)

For durations:

  • "d" = days
  • "h" = hours
  • "m" = minutes
  • "s" = seconds
SPSS date formats as applied to an example date/time of Thursday, January 31, 2013, 1:02:33.7 (AM).
Format Example
dd-mmm-yy 31-JAN-13
dd-mmm-yyyy 31-JAN-2013
mm/dd/yy 01/31/13
mm/dd/yyyy 01/31/2013
dd.mm.yy 31.01.13
dd.mm.yyyy 31.01.2013
yyddd 13031
yyyyddd 2013031
yy/mm/dd 13/01/31
yyyy/mm/dd 2013/01/31
q Q yy 1 Q 13
q Q yyyy 1 Q 2013
mmm yy JAN 13
mmm yyyy JAN 2013
ww WK yy 5 WK 13
ww WK yyyy 5 WK 2013
(name of the day) THU
(name of month) JAN
hh:mm 1:02
hh:mm:ss.s 01:02:33.7
dd hh:mm 31 01:02
dd hh:mm:ss.s 31 01:02:33.7
dd-mmm-yyyy hh:mm 31-JAN-2013 01:02
dd-mmm-yyyy hh:mm:ss.s 31-JAN-2013 01:02:33.7

Setting the Century Range for Two-Digit Years

Century range refers to the range of dates in a given century that SPSS will recognize when it reads date variables that have two digits representing the year. (For example, the year 1950 might be represented as “50” in a date variable). When years are defined by just two digits, by default SPSS treats these dates as if they fall between 1944 and 2043 (e.g., “50” would indicate “1950”). However, some data—such as archival data—may include dates that precede this default range. Thus, for this type of data, you may want to change the century range for two-digit years.

Why does the century range matter? If you ever decide to change the format of your date variables—for example, from a format that uses a two-digit year to a format that uses a four-digit year—the years may display in the wrong century in the new format.

To change the century range for two-digit years, follow these steps:

  1. Click Edit > Options.

  2. The Options window will appear. Click the Data tab at the top.

  3. On the right-hand side you will see the Set Century Range for 2-Digit Years area.

    By default, Automatic will be selected and two-digit years will be understood to fall in the range of 1944 to 2043. You can change the century range by clicking Custom, which will allow you to input a new beginning year (and the end year will be imputed for you). When you are finished, click Apply, and then click OK.

Defining Date-Time Variables in the Variable View Tab

It is important to specify which variables in your data are dates/ times so that SPSS can recognize and use these variables appropriately. However, the procedure for defining a variable as date/time depends on its currently defined type (e.g., string, numeric, date/time). The following sections outline how to define a variable as date/time based on the variable’s current type.

Changing a variable type from string or numeric to date/time

If your dataset includes a variable whose values represent dates or time, but the variable is currently defined as string or numeric, you should specify that the variable is actually a date/time. You can specify the variable type as date/time by clicking the Variable View tab, locating the variable, and clicking on the cell beneath the “Type” column. A blue “…” button will appear. Clicking the blue “…” button opens the Variable Type window. Select “Date” from the list of variable types. Then, on the right, select the format in which the date/time for that variable should appear (by selecting the date/time format in which the values already appear). Click OK. Now SPSS will recognize the variable as date/time.

Note: These steps work only if the variable values are already in a standard date/time format but are currently defined as string/numeric…and only if you define the variable as date/time by selecting a date/time format that already mirrors the existing format. For example, if the values appear as “Aug 1991” you should select a date/time format that mirrors the existing format. If you try to select a format that includes additional or different information, the change in format may fail and blank out the data.

Example: This scenario is likely if you import data from another file source, such as Excel, and SPSS does not immediately define the variable type as date/time, even though the values are in a standard date/time format.

Thus, the following criteria must apply in order to use the steps outlined above:

  1. Your variable values already appear in a standard date/time format.
  2. Your variable is currently defined as “string” or “numeric” rather than date/time.
  3. You wish to re-define your variable type from string or numeric to date/time.

Changing the variable type from string or numeric to a date/time format that is different from the date/time format in which the values currently appear

If the variable is already in a standard date/time format but is currently defined as string or numeric, and you wish to both A) define the variable as date/time, and B) choose a different date/time format than the one that matches the current format, you must proceed in two steps.

  1. You must first define the variable as date/time and select the format in which your dates/times currently appear.
  2. After you have specified the current format of date/time values for that variable, you can then change the format of the date following the same steps you used to define the variable type and date format during the first step.

Note: If the dates for a selected variable appear as mm/dd/yyyy and are currently defined as “String” in the “Type” of variable in Variable View, you cannot change the “Type” to “Date” and select the new format in which you want the date/time values to appear. You must first select the format in which the dates/times currently appear. Then, you can repeat this process to select the new format in which you want the dates to appear. If you do not first define a variable as a “Date” and select the current date/time format before selecting the format to which you want to change it, the values for that variable will be defined as missing.

Example: If a variable with date/time values is currently defined as string or numeric, but all the values follow the form mm/dd/yyyy (e.g., 01/31/2013), then you must select this format (mm/dd/yyyy) when you change the variable’s type to date/time. Do not select a format that does not match the current format of the values.

Thus, the following criteria must apply in order to use the steps outlined above:

  1. Your variable values already appear in a standard date/time format.
  2. Your variable is currently defined as “string” or “numeric” rather than date/time.
  3. You wish to re-define your variable type from string or numeric to a date/time format that is different from the date/time format in which the values currently appear.

Changing variables defined as dates/time from one date format to another date format

If a variable type is already defined as date/time, then changing the format of the values to a different date/time format is simple. In Variable View, under the column “Type,” select the cell that corresponds to the variable you want to change. A blue “…” button will appear, which opens the “Variable Type” dialog box. “Date” should already be selected from the list of variable types on the left. On the right, select the new date/time format in which you would like the variable values to appear. Click OK. Now click the Data View tab to view your data; your dates should now appear in the format you selected.

Note: If you select a new format that includes space for information that does not actually appear in your dataset, it will appear as 0s in the data. For example, if your data only includes information about the month, day, and year, and you select a format that also includes space for the hour, minute, and second, values will appear like this one: 31-JAN-2013 00:00:00.

Example: Perhaps your date is defined as date/time and appears as “01/31/2013,” but you would like it to appear as “2013/01/31,” instead.

Date and Time Wizard

SPSS conveniently includes a Date and Time Wizard that can assist with transformations and calculations that involve date and time variables. To access the Date and Time Wizard, click Transform > Date and Time Wizard.

The Date and Time Wizard window will appear.

Although there are many options, it is useful to begin by first reading about how dates and times are represented in SPSS. We have selected this option (Learn how dates and times are represented) in the Date and Time Wizard window (depicted above). Now, click Next. You will see the following window.

Window titled Date and Time Wizard Step 1 of 1. It only contains explanatory text, which reads: Date/Time Variables in SPSS Statistics represent dates, times, and durations. They are numeric variables with special display formats that you can change. The numeric value of a date or time is the number of seconds since midnight, October 14th, 1582. They can be used in transformations and procedures. $TIME is a special variable that can be used in transformations and holds the current date and time. You can extract portions of a date such as the year or the hour and create dates from calendar units. You can convert between date strings and numeric data values.

When you are finished reading, click Back to return to the main Date and Time Wizard menu.

Note that the Date and Time Wizard can assist with many tasks related to dates and time, including:

  • Creating a date/time variable from a string containing a date or time
  • Creating a date/time variable from variables that contain parts of dates or times
  • Calculating with dates and time
  • Extracting parts of dates or time
  • Assigning periodicity to a dataset for time series data

We will not cover each of these options in this tutorial, but we will cover one of the most common uses for the Date and Time Wizard: calculations involving dates and times.

Example: Converting a string variable to a date variable

Problem Statement

If you have datetime variables in a text or CSV file, SPSS will often read those variables in as string or character variables, instead of treating them as actual dates. In order to have those variables correctly recognized, you'll need to convert them from string to date.

In the sample dataset, the variable enrolldate (date of college enrollment) contains dates in the form dd-mmm-yyyy, but was read into the dataset as a string variable. Let's convert that variable from a string to a numeric date.

Running the Procedure

Using the Date & Time Wizard

  1. Click Transform > Date and Time Wizard.
  2. Select Create a date/time variable from a string containing a date or time. Then click Next.
  3. In the Variables box, select variable enrolldate. This will show a preview of the values of the variable in the Sample Values box, so that you can select the correct pattern. In the Patterns box, click dd-mmm-yyyy. Then click Next.

  4. In the Result Variable box, enter a name for the new date variable; let's call it date_of_enrollment. Optionally, you can add a variable label, and if desired, you are able to change the date format used for the output variable.

  5. Click Finish.

Using Syntax

COMPUTE date_of_enrollment=number(enrolldate, DATE11).
VARIABLE LABELS date_of_enrollment 'Date of college enrollment'.
VARIABLE LEVEL  date_of_enrollment (SCALE).
FORMATS date_of_enrollment (DATE11).
VARIABLE WIDTH  date_of_enrollment(11).
EXECUTE.

What's going on in this syntax?

  • The first line (COMPUTE) actually computes the new date variable using the built-in function number(), which converts string variables to numeric variables. The argument DATE11 tells SPSS that the content of the string variable is in DATE11 format initially (dd-mmm-yyyy).
  • The second line (VARIABLE LABELS) applies the variable label "Date of college enrollment" to the new variable.
  • The third line (VARIABLE LEVEL) explicitly sets the measurement level of the new variable to Scale.
  • The fourth line (FORMATS) applies a human-readable date format to the new variable. Here, we tell SPSS to continue using the DATE11 (dd-mmm-yyyy) format for the new variable.
  • The fifth line (VARIABLE WIDTH) tells SPSS how wide the column should be.  This particular date format always has 11 characters, so the column is set to have width 11.

Example: Computing Elapsed Time between Two Date-Time Variables

Problem Statement

Sometimes you may need to calculate the length of time that has passed between two points in time. For example, you may wish to calculate the ages of people in your sample based on information you have about when they were born and what the current day/time/year is (or another date of your choosing). Any unit of time can be used. This means that you can calculate how many years, months, days, hours, minutes, or even seconds old each person is. 

Before we can perform a calculation with dates and times, we first need to make sure that our dataset has at least two variables that represent time points. If you completed the above example, you will now have at least two date variables in the sample dataset: bday (the person's date of birth) and now date_of_enrollment (the date the person enrolled in college). We can compute the age that each person was when they enrolled in college using these two time points.

Running the Procedure

Using the Date & Time Wizard

  1. Click Transform > Date and Time Wizard. The Date and Time Wizard window will appear.
  2. Click Calculate with dates and times and then click Next.

  3. Click Calculate the number of time units between two dates. Click Next.

  4. We will now specify how the new variable should be computed from our existing date variables in this calculation: date_of_enrollment (the date at which a person enrolled in college) and bday (the date of birth).

    AVariables: Lists all of the available date and time variables in your dataset. It also includes a variable called “$TIME” which represents the current date and time.

    BDate1: The right half of the dialog box is where we will specify which variables to use, and how to set up the calculation. In the Date1 field, select the variable date_of_enrollment and in the minus Date 2 field, select the variable bday. This specifies that SPSS should calculate date_of_enrollment minus bday, which will yield the number of years between when the person was born and when they enrolled in college (i.e., their age at college enrollment).

    CUnit: The unit of time to use for the variable you are creating. You can choose among Years, Months, Weeks, Days, Hours, Minutes, and Seconds. In this example, select Years from the Unit list.

    DResult Treatment: Specify how to treat the values of the variable that will be calculated. You can choose to truncate to integer, round to integer, or retain fractional part.
    Truncate to integer means dropping the fractional part (e.g., 1.3 would become 1, and 1.6 would become 1).
    Round to integer bumps the number to the nearest integer (e.g., 1.3 would round to 1, but 1.6 would round to 2).
    Retain fractional part means that the fraction will remain (e.g., 1.6 remains 1.6).
    In this example, fractions will be retained so that the values for the new variable reflect the individual's exact age (in years) when they enrolled in college.

    When you are finished setting up the calculation, click Next

  5. The final window asks you to name the variable you are creating in the Result Variable field and to provide a label in the Variable Label field. Here, the new variable is called age_at_enrollment and is labeled "Age at time of college enrollment".

    The Execution area allows you to choose how to create the new variable. You can have SPSS Create the variable now, which will immediately create the new variable in your dataset. Alternatively, select Paste the syntax into the syntax window, which will have SPSS write the syntax (command language) that will create the variable whenever you choose to run the syntax command in the future. This latter option will not create the new variable until you run the syntax.

  6. When you are finished, click Finish.

Once your new variable has been created, it is always a good idea to check that the calculation was accurate. You can do this by spot-checking some of the rows in your data. You can manually calculate the time between date_of_enrollment and bday for some of the cases in the data and then compare the manual calculation to the value SPSS created in the new variable age_at_enrollment.

Using Syntax

COMPUTE age_at_enrollment=(date_of_enrollment - bday) / (365.25 * time.days(1)).
VARIABLE LABELS age_at_enrollment "Age at time of enrollment (years)".
VARIABLE LEVEL age_at_enrollment (SCALE).
FORMATS age_at_enrollment (F8.2).
VARIABLE WIDTH  age_at_enrollment(8).
EXECUTE.

What's going on in this syntax?

  • The first line (COMPUTE) performs the calculation of the elapsed times. Notice that the calculation isn't simply the difference of the two date variables: in the denominator, the term (365.25*time.days(1)) corrects for different year lengths due to leap years.
  • The second line (VARIABLE LABELS) applies the variable label "Age at time of enrollment (years)" to the new variable.
  • The third line (VARIABLE LEVEL) explicitly sets the measurement level of the new variable to Scale.
  • The fourth line (FORMATS) tells SPSS that the computed variable is a numeric variable that has two decimal places and is at most 8 characters wide.
  • The fifth line (VARIABLE WIDTH) sets the width of the variable to 8 characters.
  • The last line (EXECUTE) tells SPSS to carry out the computation and add the new variable to the active dataset. (Without this line, SPSS will create the variable in the computer's memory but not actually add it to the dataset.)