IBM Support

Converting non-date formatted variables into date format in SPSS

Question & Answer


Question

You have a date variable in a non-date format and want to convert it into a date formatted variable using SPSS. How do you accomplish accomplish this?

Answer

The following SPSS command syntax examples show you how to convert several types of non-date formatted variables into date formatted variables.
You may also use the Date and Time Wizard, available since SPSS 14 from the menu Transform->Date and Time Wizard.

*1) You imported a data file from Excel to SPSS. Your date value is read in as a string.
*How can you convert the string into a mm/dd/yy format?

DATA LIST/v1 (a8).
BEGIN DATA
01/02/98
05/25/97
03/16/98
END DATA.
EXECUTE.

COMPUTE datevar=NUMBER(v1,ADATE8).
FORMATS datevar (ADATE8).
LIST.

*2) You imported a file from another program into SPSS. You had a date field that appears as a *number in the SPSS Data Editor window. How can you turn this into a mm/dd/yy format?

DATA LIST /v1 (F6.0).
BEGIN DATA
100298
052597
031698
END DATA.
EXECUTE.

*Computes the year, day, and month from the string variable.

COMPUTE year=number(SUBSTR(string(v1,F8),7,2),F2).
COMPUTE day=number(SUBSTR(string(v1,F8),5,2),F2).
COMPUTE month=number(SUBSTR(string(v1,F8),3,2),F2).
COMPUTE date=DATE.MDY(month,day,year).
FORMAT date(ADATE8).
LIST.
EXECUTE.

*Saves the data file and drops the original variable along with the year, day, and month *variable.
SAVE OUTFILE='dum.sav' / DROP=v1 year day month.
GET FILE='dum.sav'.

*3) You have two date fields, an entrance and an exit date. You are trying to compute the number of *days between the two dates. When you subtract the two, you get a really strange number. What is *going on and how do you fix it?

DATA LIST Free/enter (adate8) exit (adate8).
BEGIN DATA
02/05/98 02/07/98
03/16/97 03/25/97
05/23/98 05/25/98
12/02/98 12/25/98
END DATA.
EXECUTE.

*Computes the difference between the two dates.
COMPUTE diff = exit - enter.
EXECUTE.

*Converts the variable to number of days.
COMPUTE dif = CTIME.DAYS (diff).
EXECUTE.

*or you can just do this instead of taking the difference:

COMPUTE dif = CTIME.DAYS(exit-enter).
LIST.

*4) In your data set, you have a date variable and numeric variable. The numeric variable *represents a number of days and can be different for each case. You would like to add the *number of days to the date variable to produce a new date variable.

DATA LIST Free/v1(adate8) v2(F2.0).
BEGIN DATA
02/05/98 2
03/16/97 27
05/23/98 5
12/02/98 12
END DATA.
EXECUTE.

COMPUTE v3=v1 + time.days (v2).
FORMATS v3 (adate8).
LIST.

*5) You have a date variable that represents a birth date. How can you figure out the age in *years from today's date?

DATA LIST /v1 (adate8).
BEGIN DATA
10/02/98
05/25/97
03/16/98
END DATA.
EXECUTE.

*Computes today's date and then subtracts the birthday from today's date.
COMPUTE TODAY=XDATE.DATE($TIME).
COMPUTE AGE=(CTIME.DAYS(TODAY-v1))/365.25.
EXECUTE.

*6) you have a date as a string with month and year. How can you get it into MOYR6 format?

NEW FILE.
DATA LIST Free/ date (a4).
BEGIN DATA
0597
0898
1075
1294
0995
END DATA.

*Creates 2 string variables for month and year, converts them to numeric and creates the new *variable in the desires format.

STRING year (a2).
COMPUTE year=SUBSTR(date,3,2).
STRING month (a2).
COMPUTE month=SUBSTR(date,1,2).
COMPUTE year1 = number (year,F2.0).
COMPUTE month1 = number (month,F2.0).
COMPUTE newdate = DATE.MOYR(month1,year1).
EXECUTE .
FORMATS newdate (MOYR6).

*7)
NEW FILE.
DATA LIST / datex (a8).
BEGIN DATA
19950502
19940323
19931229
19920205
END DATA.

*Computes a variable for month, day, and year.
COMPUTE mo=number(SUBSTR(datex,5,2),F2).
COMPUTE da=number(SUBSTR(datex,7,2),F2).
COMPUTE yr=number(SUBSTR(datex,1,4),F4).
COMPUTE date=DATE.MDY(mo,da,yr).
FORMAT date(ADATE8).
LIST.
EXECUTE.
EXECUTE.

*8) You have a date in ADATE8 format that you would like to convert to a string (A8) format as *YYYYMMDD. How do you do that?

*This is the sample data.
DATA LIST /datevar (adate8).
BEGIN DATA
10/02/98
05/25/97
03/16/98
END DATA.
EXECUTE.

*This creates a dummy string variable.
FORMATS datevar (SDATE10).
STRING strdate (A10).
COMPUTE strdate = STRING(datevar,SDATE10).
EXECUTE.

*This creates the desired variable.
STRING new (A8).
COMPUTE
new=concat(SUBSTR(strdate,1,4),SUBSTR(strdate,6,2),SUBSTR(strdate,9,2)).
EXECUTE.

*If you want to get rid of the "strdate" dummy variable.
SAVE OUTFILE='dum.sav' / DROP=strdate.
GET FILE='dum.sav'.

*9) You have a date in the format DATE10 (dd-mmm-yy) and you would like to recode it into numeric *values.

DATA LIST /datevar (date10).
BEGIN DATA
02/07/98
02/07/98
12/02/98
12/02/98
01/10/75
25/05/79
01/11/75
25/05/79
END DATA.
EXECUTE.

*Converts 02/JUL/98 to 07/02/98.
FORMATS datevar(ADATE8).
EXECUTE.

*10) You have a string date variable as seen in the example below. You want to change this to be a *date variable in SPSS.

DATA LIST /date(A10).
BEGIN DATA
2-OCT-98
2/JUL/98
12/AUG/98
12-SEP-98
1-MAY-75
END DATA.
EXECUTE.

*Computes length to differentiate between the single digit months and 2 digit months.
COMPUTE length = INDEX(date,' ').

STRING month(A3).
*Breaks out the date variable into day, month, and year variables.

DO IF (length = 9).
COMPUTE month = SUBSTR(date,3,3).
COMPUTE day=number(substr(date,1,1),f2).
COMPUTE year=number(substr(date,7,2),f2).

ELSE IF (length = 10).
COMPUTE month = SUBSTR(date,4,3).
COMPUTE day=number(substr(date,1,2),f2).
COMPUTE year=number(substr(date,8,2),f2).
END IF.

*Recodes month in to numeric month.
RECODE
month
('JAN'=1) ('FEB'=2) ('MAR'=3) ('APR'=4)
('MAY'=5) ('JUN'=6) ('JUL'=7) ('AUG'=8)
('SEP'=9) ('OCT'=10) ('NOV'=11) ('DEC'=12) INTO month2 .

*Computes the new date.
COMPUTE date2 = DATE.DMY(day,month2,year).
FORMATS date2(ADATE8).

EXECUTE.

*Drops all the extra variables.
SAVE OUTFILE='dum.sav' / DROP=length year day month month2.
GET FILE='dum.sav'.

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF016","label":"Linux"},{"code":"PF014","label":"iOS"},{"code":"PF033","label":"Windows"}],"Version":"Not Applicable","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

21564

Document Information

Modified date:
16 April 2020

UID

swg21476991