### SPSS FAQ How can I convert string variables into date variables?

Sometimes date data have been entered as string variables, and these variables need to be converted into numeric variables.  Date variables are numeric variables in SPSS, and as such, they can be added, subtracted, etc.  Specifically, date variables in SPSS are the number of seconds since the beginning of the Gregorian calendar, which was October 14, 1582.

Let's look at an example data set below.  We see that we have date data entered as string variables in three different ways.  The examples below will show how to convert each of these into date (numeric) variables that can used in calculations.

data list list
/day1 (a2) month1 (a2) year1 (a4) date2 (a12) date3 (a12).
begin data.
12 06 2005 06/12/2005 06-Dec-2005
14 05 2004 05/14/2004 05-May-2004
01 01 1998 01/01/1998 01-Jan-1998
end data.

list.
day1 month1 year1 date2        date3

12   06     2005  06/12/2005   06-Dec-2005
14   05     2004  05/14/2004   05-May-2004
01   01     1998  01/01/1998   01-Jan-1998

Number of cases read:  3    Number of cases listed:  3

#### Example 1

In the example below, we work with the variable date3.  If your date variable is entered exactly like this, then you can use the numeric function to convert it into a numeric variable.  We use the compute command with the numeric function to create a new variable called your_date that is a numeric version of the string variable date3.  We then create a copy of your_date (called your_date1).  We use the formats command to give your_date and your_date1 different formats, so that you can see what number is associated with each of the dates displayed in your_date1.  To be clear:  your_date and your_date1 are the same variables formatted differently.

compute your_date = numeric(date3, date11).
compute your_date1 = your_date.
format your_date (f14.0) your_date1 (date12).
exe.

list.
day1 month1 year1 date2        date3             your_date   your_date1

12   06     2005  06/12/2005   06-Dec-2005     13353206400 06-DEC-2005
14   05     2004  05/14/2004   05-May-2004     13303094400 05-MAY-2004
01   01     1998  01/01/1998   01-Jan-1998     13102992000 01-JAN-1998

Number of cases read:  3    Number of cases listed:  3

#### Example 2

In this example, we will work with the three variables day1, month1 and year1.  First, we will make numeric versions of these variables using the compute command with the numeric function.  Next, we use the compute command with the date.dmy function to combine the numeric variables into a single date variable.  The date.dmy function requires numeric variables as arguments, so we must make numeric versions of the string variables for use with this function.  We then use the formats command to format the new date variable (called my_date).  Note that the execute command (shorted to exe.) is needed after the formats command, or the next command will not run.  The delete variables command is used to remove the unneeded variables from the data set.

compute dn = numeric(day1, f4.0).
compute mn = numeric(month1, f4.0).
compute yn = numeric(year1, f4.0).
exe.

compute my_date = date.dmy(dn, mn, yn).

formats my_date (date11).
exe.
delete variables dn mn yn.

list.
day1 month1 year1 date2        date3             your_date   your_date1     my_date

12   06     2005  06/12/2005   06-Dec-2005     13353206400 06-DEC-2005  12-JUN-2005
14   05     2004  05/14/2004   05-May-2004     13303094400 05-MAY-2004  14-MAY-2004
01   01     1998  01/01/1998   01-Jan-1998     13102992000 01-JAN-1998  01-JAN-1998

Number of cases read:  3    Number of cases listed:  3

#### Example 3

This example is very similar to Example 2, except that the date is contained in a single string variable, called date2.  Because the whole date is contained in the string variable date2, we need to start by breaking date2 into three parts:  month, day and year.  To start this process, we first create the three string variables (called m1, d1 and y1).  Next, we populate the new string variables using the compute command with the substr function.  In the third step, we create numeric versions of the string variables using the compute command with the numeric function.  After that, the compute command with the date.dmy function is used to combine the numeric versions of the day, month and year variables (dn, mn and yn).  The date.dmy function can only take numeric variables as arguments, so we could not use the string versions of these variables.  In the last step, we format the new date variable, called new_date, with the formats command.  We selected the adate11 format, but you could use any date format that you like.  Note that the execute command (shorted to exe.) is needed after the formats command, or the next command will not run.  We also used the delete variables command to remove the unneeded variables from data set.

string m1 (a2).
string d1 (a2).
string y1 (a4).
exe.

compute m1 = substr(date2, 1, 2).
compute d1 = substr(date2, 4, 2).
compute y1 = substr(date2, 7, 4).

compute mn = numeric(m1, f4.0).
compute dn = numeric(d1, f4.0).
compute yn = numeric(y1, f4.0).

compute new_date = date.dmy(dn, mn, yn).

exe.
delete variables d1 m1 y1 dn mn yn.
exe.
list.
day1 month1 year1 date2        date3             your_date   your_date1    new_date

12   06     2005  06/12/2005   06-Dec-2005     13353206400 06-DEC-2005  06/12/2005
14   05     2004  05/14/2004   05-May-2004     13303094400 05-MAY-2004  05/14/2004
01   01     1998  01/01/1998   01-Jan-1998     13102992000 01-JAN-1998  01/01/1998

Number of cases read:  3    Number of cases listed:  3