SPSS Tutorials


Convert Unix Dates into SPSS Dates


Dates in Unix format may enter SPSS in two flavors: as a string (for example "2011-06-20 18:15:08" from Google Analytics) or as a number ("1382064174"). These formats are easily converted to SPSS datetime variables when using the right tools.

1. Unix Timestamp as a String

SPSS Syntax Example

*1. Create test data.

data list free/timestamp(a25).
begin data
"2011-06-20 18:15:08" "2011-06-20 18:20:18"
end data.

*2. Separate date and time.

str date time(a10).
compute date = substr(timestamp,1,index(timestamp," ")).
compute time = substr(timestamp,index(timestamp," ") + 1).

*3. Convert date and time into SPSS formats.

alter type date(a10=sdate10).
alter type time(a10=time10).

*4. Reunite date and time and display as SPSS datetime format.

compute datetime = date + time.
formats datetime(datetime22).

*5. Optionally, delete auxiliary variables.

delete variables date time.

2. Unix Timestamp as a Number

SPSS Datesum Syntax Example

*1. Create test data.

data list free/timestamp.
begin data
end data.

*2. Convert to SPSS datetime format.

compute datetime = datesum(date.dmy(1,1,1970),timestamp,'seconds').
formats datetime(datetime22).

Previous tutorial: Convert Numeric to Date Variable

Next tutorial: Extract a Year from a Date

Let me know what you think!

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