# SPSS Tutorials

BASICS REGRESSION T-TEST ANOVA CORRELATION

# 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 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

• The `EPOCH` setting persists over sessions. So closing and reopening SPSS does not reset `EPOCH` to `AUTOMATIC`.
• Simply running `SHOW EPOCH.` will show the current `EPOCH` setting in the output viewer as shown in the previous syntax example.

# Let me know what you think!

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

# This tutorial has 1 comment!

• ### By Sidsel on January 4th, 2016

Hi! I'm working in a rather large dataset containing a lot of birth dates, and attempting to reformat these two-digit dates to a DD.MM.YYYY-format. I have tried and supposedly succeeded (so says the output) in setting epoch to the 1900s. However, a lot of the dates STILL show up as 2000s dates. Your correction for dates in the past worked (and thank you ever so much for that!) but I'm curious as to whether you can explain why the set epoch command didn't. The dates were not consistent, either - some DD.MM.39 dates ended up as 1939, some as 2039. It's baffling.