NOTE: This page was created using SPSS version 17.0.2. Some of the syntax shown below may not work in earlier versions of SPSS.
Most of the information regarding date and time formats can be found in the "Universals" section of the SPSS Command Syntax Reference under the heading "Date and Time Functions" and "Date and Time Formats" (page 62 for SPSS version 17).
Inputting dates
There are many ways to input dates in SPSS, and a few of them are shown below. When specifying date data, you need to have a format for each of the variables. Date formats include date, adate, edate, jdate and sdate; n is one of the numeric formats. The date format is an international date; the "a" in adate means "American"; the "e" in edate means "European"; the "j" mean Julian; the "s" means sortable. Note that when entering European dates, the day is the first number given, followed by the month and the year. (See the fifth line for an example.) The sdate date format is used in many Asian countries and is sortable in its character form. The numbers with the n specify the length of the numeric variable. The default is 1. To be extra clear, the formats listed on the data list command tell SPSS what to expect when reading in the data. If you want to display the date variables in a particular format, you can use the formats command after the data have been read in.
data list list / id (n) day (n2) month (n2) year (n4) c_date (adate) dob (edate). begin data. 1 23 11 1997 2/2/2006 6.9.1956 2 17 9 1985 3/2/2005 5.7.1966 3 30 12 2008 7/14/2005 11.11.1911 4 5 5 2004 12/6/2006 2.8.1922 5 9 9 1999 8/27/2007 14.3.1974 end data. list.id day month year c_date dob 1 23 11 1997 02/02/2006 06.09.1956 2 17 9 1985 03/02/2005 05.07.1966 3 30 12 2008 07/14/2005 11.11.1911 4 5 5 2004 12/06/2006 02.08.1922 5 9 9 1999 08/27/2007 14.03.1974 Number of cases read: 5 Number of cases listed: 5
The example below is very similar to the one above, except that we have defined the variable dob as an adate. The number 2 before the adate indicates that the 2 date variables listed before are adates.
data list list / id (n) expt_date dob (2adate) day (n2) month (n2) year (n4) year1 (adate). begin data. 1 2/2/2006 6/9/1956 23 11 1997 1/1/1907 2 3/2/2005 5/7/1966 17 9 1985 9/5/1908 3 7/14/2005 11/11/1911 30 12 2008 9/3/1912 4 12/6/2006 2/8/1922 5 5 2004 2/7/1916 5 8/27/2007 3/14/1974 9 9 1999 11/18/1980 end data. list.id expt_date dob day month year year1 1 02/02/2006 06/09/1956 23 11 1997 01/01/1907 2 03/02/2005 05/07/1966 17 9 1985 09/05/1908 3 07/14/2005 11/11/1911 30 12 2008 09/03/1912 4 12/06/2006 02/08/1922 5 5 2004 02/07/1916 5 08/27/2007 03/14/1974 9 9 1999 11/18/1980 Number of cases read: 5 Number of cases listed: 5
In the next example, we will show some different date formats as well as various ways of specifying the dates themselves. The values of the months can be entered as numbers, three letter abbreviations, Roman numerals or fully spelled out. Years can be specified as either two or four digits. To change the century cut off, use the set epoch command. By default, the current century century range begins 69 years prior to the current year and ends 30 years after the current year.
In the input below, three different types of formats are used. The variables first_date and second_date are formatted as date. This means that the values of the day, month and year are separated by a slash, period, dash, comma or space. The variable third_date is formatted as an adate. Notice that the month can even been entered as Roman numerals (as shown on the first line). The variable fourth_date is formatted as a jdate (a Julian date). When specifying a date as a jdate, the year is given first (either as a two or four digit number) followed by the number of the day of the year. In our example, we use December 22, which is 356th day of the year; June 30 is the 181st day of the year. (Please see the yrmoda function below to determine the number of the day of the year.)
NOTE: When entering fixed format data into the syntax file, it is very important that each data value starts and ends within the specified field. To be sure in which column a value is starting and ending, put the curser at the beginning (or ending) of a value, and then look in the lower right-hand corner of the syntax window. Values that are in the same column may appear to be unaligned in the syntax window.
data list fixed / first_date 1-13 (date) second_date 20-36 (date) third_date 37-50 (adate) fourth_date 52-60 (jdate). begin data. 30/6/03 22 December 1990 XII 22 1990 1990356 30-6-03 22/12/90 12/22/90 90181 30 June, 2003 22.12.1990 Dec. 22, 1990 03181 30.Jun.03 22-DEC-1990 June 30, 03 2003181 end data. list.first_date second_date third_date fourth_date 30-JUN-2003 22-DEC-1990 12/22/1990 1990356 30-JUN-2003 22-DEC-1990 12/22/1990 1990181 30-JUN-2003 22-DEC-1990 12/22/1990 2003181 30-JUN-2003 22-DEC-1990 06/30/2003 2003181 Number of cases read: 4 Number of cases listed: 4
For information about working with dates entered as strings, please visit How can I convert string variables into date variables?
Formatting dates for display
There are many date formats that you can use to display dates in your data file. To use them, you issue the formats command, followed by the variable or variables that you want to format, and then specify the format in parentheses after the variable to be formatted. The number given after the format indicates the length. Let’s use the data set that we just read into SPSS and assign different formats to the variables. As with input formats, the "a" in adate indicates American, the "e" in edate indicates European, etc.
formats first_date (adate8) second_date (adate10) third_date (edate10) fourth_date (date10). list.first_date second_date third_date fourth_date 06/30/03 12/22/1990 22.12.1990 22-DEC-90 06/30/03 12/22/1990 22.12.1990 30-JUN-90 06/30/03 12/22/1990 22.12.1990 30-JUN-03 06/30/03 12/22/1990 30.06.2003 30-JUN-03 Number of cases read: 4 Number of cases listed: 4
Because dates are simply numeric variables, we can also assign a numeric format to a date variable. You may want to do this if you need to see the actual numeric value of the date variable. For example, let’s suppose that you want to create a a filter variable such that only dates after a given date are included in the analysis. To create the filter variable, you need to know the numeric value of the date you want to use as the cutoff. In the example below, we will use June 30, 2003 as our cutoff date for creating the filter variable. To make it easy to see the correspondence between the date and its numeric value, we will create a copy of the date variable and format it with a numeric format.
compute td = third_date. formats td (f12.0). list third_date td.third_date td 12/22/1990 12881203200 12/22/1990 12881203200 12/22/1990 12881203200 06/30/2003 13276310400 Number of cases read: 4 Number of cases listed: 4
In the output above, we can see that 12881203200 corresponds to December 22, 1990 and 13276310400 corresponds to June 30, 2003.
For more information on formats, please see Introduction to SPSS Syntax, Part 2 (section 2).
Manipulating dates
We will use the following data set for our examples.
data list list / id (n) expt_date dob (2adate) day (n2) month (n2) year (n4) year1 (adate). begin data. 1 2/2/2006 6/9/1956 23 11 1997 1/1/1907 2 3/2/2005 5/7/1966 17 9 1985 9/5/1908 3 7/14/2005 11/11/1911 30 12 2008 9/3/1912 4 12/6/2006 2/8/1922 5 5 2004 2/7/1916 5 8/27/2007 3/14/1974 9 9 1999 11/18/1980 end data.
Now that we have input data, let’s start working with the date variables. Dates are simply numeric variables in SPSS. Because "Time 0" was a long time ago, the numeric values of current dates in SPSS are really large. Dates are stored as the number of seconds from midnight, October 14, 1582 (the beginning of the Gregorian calendar). Therefore, you usually need to do some math in order to calculate the number of days (or months or years) between two dates. (Sometimes it is handy to know that there are 86,400 seconds in a day.) Because dates are stored as numbers, you can do standard mathematical operations on them, such as adding or subtracting them. If your date is displayed as stars or if only part of the year is showing in the SPSS Data Editor, you can make the column wider and the dates will display properly.
First, we will create a new variable called diff, which will be the difference between expt_date and dob. As you can see in the output, diff is a numeric variable (as are expt_date and dob). With the second compute command we will create a new variable called age that will be the subjects’ current age. We will also include a formats command to format the variable diff so that it is not displayed in scientific notation.
compute diff = expt_date - dob. compute age = diff/(60*60*24*365.25). formats diff (f16.0). list expt_date dob diff age.expt_date dob diff age 02/02/2006 06/09/1956 1566864000 49.65 03/02/2005 05/07/1966 1225065600 38.82 07/14/2005 11/11/1911 2956089600 93.67 12/06/2006 02/08/1922 2676844800 84.82 08/27/2007 03/14/1974 1055721600 33.45 Number of cases read: 5 Number of cases listed: 5
Functions
There are many functions that can be used to aid in the manipulation of dates. These functions are grouped into six categories: aggregate, conversion, yrmoda (year month day), extraction, date difference and date increments. Aggregation functions generate date and time intervals from values that were not read by date and time input formats, and they begin with date or time. The conversion functions convert time intervals from one unit of time to another, and they begin with ctime. There is only one yrmoda function, and it converts year, month, and day to a day number. The number returned is the number of days since October 14, 1582 (day 0 of the Gregorian calendar). This is useful if you need to work with Julian dates. The extraction functions are used to extract a day or time from a date variable. These functions start with xdate, followed by a period and the name of the subfuction. The date difference (datediff) function calculates the difference between two date values and returns an integer in the specified date/time units. The datesum function calculates a date or time value a specified number of units from a given date or time value.
The aggregate functions
We will start by looking at some of the aggregate functions. These functions combine two or more variables into a single variable. In the example below, the date.dmy function is used to combine the variables day, month and year into a single variable called date1. The formats command is used to format the variables day, month and year (so that they look nicer in the output), and the variable date1 so that it is not displayed in scientific notation.
compute date1 = date.dmy(day,month,year). formats day month year (f4.0) date1 (f16.0). list day month year date1.day month year date1 23 11 1997 13099622400 17 9 1985 12715142400 30 12 2008 13449974400 5 5 2004 13303094400 9 9 1999 13156214400 Number of cases read: 5 Number of cases listed: 5
We can also use the formats to display date1 as a date.
formats date1 (adate10). list day month year date1.day month year date1 23 11 1997 11/23/1997 17 9 1985 09/17/1985 30 12 2008 12/30/2008 5 5 2004 05/05/2004 9 9 1999 09/09/1999 Number of cases read: 5 Number of cases listed: 5
The yrmoda function
The yrmoda (year-month-day) function converts the year, month and day specified into the number of days (not seconds) since October 14, 1582. For example, there have been 155,790 days since October 14, 1582.
compute today = yrmoda(2009, 04, 28). list today.today 155790.0 155790.0 155790.0 155790.0 155790.0 Number of cases read: 5 Number of cases listed: 5
The conversion functions
The conversion functions convert date (and datetime) variables into the unit given in the subfunction (which is the part after the period in the function call). In the example below, we create a filter variable (called filter1 in our example) such that only subjects with a birthday equal to or later than June 30, 1950 will be included in the analysis. As we saw above, the yrmoda function returns the number of days (a numeric value) from the date given in the function. We can use the ctime.days function to compare the values in the variable dob to the date in which we are interested (June 30, 1950).
compute filter1 = 0. if ctime.days(dob) ge yrmoda(1950, 6, 30) filter1 = 1. list. * filter by filter1.id expt_date dob day month year year1 filter1 1 02/02/2006 06/09/1956 23 11 1997 01/01/1907 1.00 2 03/02/2005 05/07/1966 17 9 1985 09/05/1908 1.00 3 07/14/2005 11/11/1911 30 12 2008 09/03/1912 .00 4 12/06/2006 02/08/1922 5 5 2004 02/07/1916 .00 5 08/27/2007 03/14/1974 9 9 1999 11/18/1980 1.00 Number of cases read: 5 Number of cases listed: 5
The extraction functions
Below we illustrate the use of some extraction functions. In the following examples, we use the xdate.year function to extract the year from each variable, and then do the subtraction of the two dates. The variable diff was created above as the difference between each subject’s date of birth (dob) and the date of the experiment (expt_date). In the first example, the variable diff is used in the xdate.year function to recalculate the subjects’ age (by subtracting 1582, the first year of the Gregorian calendar). The second compute command creates the variable age2 by using the xdate.year function to subtract the year of the subjects’ birth from the year of the experiment date. As you can see, you get slightly different results depending on you create the variable (look at the values of age1 and age2 in the last row).
compute age1 = xdate.year(diff) - 1582. compute age2 = xdate.year(expt_date) - xdate.year(dob). exe. formats diff (f16.2). list expt_date dob diff age age1 age2.expt_date dob diff age age1 age2 02/02/2006 06/09/1956 1566864000.00 49.65 50.00 50.00 03/02/2005 05/07/1966 1225065600.00 38.82 39.00 39.00 07/14/2005 11/11/1911 2956089600.00 93.67 94.00 94.00 12/06/2006 02/08/1922 2676844800.00 84.82 85.00 84.00 08/27/2007 03/14/1974 1055721600.00 33.45 34.00 33.00 Number of cases read: 5 Number of cases listed: 5
Let’s illustrate some of the other extraction functions. The xdate.week function is used to create the variable num_weeks, which is the number of weeks from the beginning of the year to the date given in the variable expt_date. The xdate.month function is used to create the variable month_num, which is the number of the month, and the xdate.quarter function is used to create the variable qtr, which is the quarter of the year.
compute num_weeks = xdate.week(expt_date). compute month_num = xdate.month(expt_date). compute qtr = xdate.quarter(expt_date). list expt_date num_weeks month_num qtr.expt_date num_weeks month_num qtr 02/02/2006 5.00 2.00 1.00 03/02/2005 9.00 3.00 1.00 07/14/2005 28.00 7.00 3.00 12/06/2006 49.00 12.00 4.00 08/27/2007 35.00 8.00 3.00 Number of cases read: 5 Number of cases listed: 5
The date difference functions
The date difference functions can be used to obtain the difference in integers between two dates. Note that the first two arguments must be variables that have been specified as dates. If one of the variables is not specified as a date variable (it is just input as a regular numeric variable), the function does not work as expected. The third argument (given in quotes) indicates the unit of time that you would like. You can specify years, quarters, months, weeks, days, hours, minutes or seconds.
compute diff_year = datediff(expt_date, year1, "years"). compute diff_weeks = datediff(expt_date, year1, "weeks"). formats diff_date diff_weeks (f4.0). list expt_date year1 diff_date diff_weeks.expt_date year1 diff_date diff_weeks 02/02/2006 01/01/1907 99 5170 03/02/2005 09/05/1908 96 5034 07/14/2005 09/03/1912 92 4845 12/06/2006 02/07/1916 90 4739 08/27/2007 11/18/1980 26 1396 Number of cases read: 5 Number of cases listed: 5
The date increment function
The date increment function (called datesum) can be used to "add" or "subtract" specified increments of time to a date. In our examples, we will use the date variable expt_date. This function requires three arguments (a fourth argument is optional). The first argument is the date variable, the second is the number of units (either positive or negative), and the third argument is the unit of time (given in quotes). The first compute command creates a new variable called ds_days that is five days after the value of expt_date. The second compute command creates a variable called ds_months, which is two months after the value of expt_date. The third compute command creates a variable called ds_years, which is 10 years before the value of expt_date. The formats command is used to format the three new variables as American dates with a length of 10.
compute ds_days = datesum(expt_date, 5, "days"). compute ds_months = datesum(expt_date, 2, "months"). compute ds_years = datesum(expt_date, -10, "years"). formats ds_days ds_months ds_years (adate10). list expt_date ds_days ds_months ds_years.expt_date ds_days ds_months ds_years 02/02/2006 02/07/2006 04/02/2006 02/02/1996 03/02/2005 03/07/2005 05/02/2005 03/02/1995 07/14/2005 07/19/2005 09/14/2005 07/14/1995 12/06/2006 12/11/2006 02/06/2007 12/06/1996 08/27/2007 09/01/2007 10/27/2007 08/27/1997 Number of cases read: 5 Number of cases listed: 5