SPSS Tutorials

BASICS REGRESSION T-TEST CHI-SQUARE TEST ANOVA

Two Digit Year in String – Cautionary Note

Summary

Converting a string variable to a date variable was discussed in Convert String to Date Variable. A caveat that was mentioned is that it's unclear to which century two digit years belong. By default, SPSS presumes all two digit years fall between 69 years prior to the current year and 30 years ahead of the current year. If this doesn't hold for your data, you can change this behavior by setting a different EPOCH.

SPSS String to Date SPSS default interpretation of two digit years

Demonstration

Running the syntax below demonstrates the aforementioned point. The first step creates a single string variable dataset for our demonstration. Next, we'll copy this variable and convert it to an SPSS date variable.
After running ALTER TYPE in step 3, all seems fine. However, when we display years as four (rather than two) digits, we'll see how SPSS interprets two digit years.

SPSS String to Date Syntax Example

*1. Create string date.

input program.
string d1(a9).
loop #year = 0 to 99.
compute d1 = concat('31-dec-',string(#year,n2)).
end case.
end loop.
end file.
end input program.
exe.

*2. Copy string date.

string d2(a9).

compute d2 = d1.
exe.

*3. Convert to SPSS date.

alter type d2(date9).

*4. Show year as four digits.

formats d2(date11).

Setting a Different Epoch

The interpretation of two digit years depends on a system setting called EPOCH. EPOCH is the first year of a 100-year time span presumed to hold all two digit years. For example, SET EPOCH 1900. causes all two digit years to be interpreted as years between 1900 and 1999.
The only exception here is SET EPOCH AUTOMATIC. which results in the default interpretation of two digit years -between 69 years prior to and 30 years after the current year. If all our date values are known to occur in the 20th century, the syntax below shows how to convert them correctly.

SPSS Set Epoch Syntax

*1. Delete previous date variable.

delete variables d2.

*2. Copy string date variable.

string d2(a9).

compute d2 = d1.
exe.

*3. For all dates in 20th century, set epoch to 1900.

set epoch 1900.

*4. Convert string to date.

alter type d2(date11).

All Dates in the Past

An alternative to resetting the EPOCH is correcting dates manually after converting them. For all dates in the past, this can be done by subtracting exactly 100 years from all dates that are greater than now (referred to by $time in SPSS). This is easily accomplished by a simple IF command as demonstrated below.

SPSS Date Correction Syntax

*1. Delete previous date variable.

delete variables d2.

*2. Copy string date variable.

string d2(a9).

compute d2 = d1.
exe.

*3. Reset epoch to default.

set epoch automatic.

*4. Check epoch.

show epoch.

*5. Convert to SPSS date.

alter type d2(date11).

*6. If all dates occur in past, use following correction.

if d2 gt $time d2 = datesum(d2,-100,'years').
exe.

Notes

Previous tutorial: SPSS Datasets Tutorial 1 – Basics

Next tutorial: SPSS Keyboard Shortcuts

Let me know what you think!

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

This tutorial has 1 comment!