Question & Answer
Question
How can I calculate the difference between two dates, resulting in days, months or years in Statistics?
Answer
The Date/Time wizard (found under Transform -> Date/Time) can calculate the difference between two dates with results in days, months or years.
When using syntax to calculate the difference between two dates, functions are used to process date values. These functions allow for flexibility while working with dates of varying formats. The following syntax examples demonstrate several ways to calculate the time between two dates.
For more information about working with Dates in Statistics, please the section on Arithmetic Operations with Date and Time Variables under Universals/Date and Time in the SPSS Command Syntax Reference.
* Sample data set to demonstrate the examples.
Data List fixed/ date1 1-10 (adate) date2 11-20 (adate).
Begin Data
06/12/199705/09/2007
12/08/199705/09/2007
10/25/199705/09/2007
04/25/198705/09/2007
02/16/199705/09/2007
03/19/199805/09/2007
End Data.
EXE.
A. Examples of Results in Days
Examples of Results in Days.
*Subtract the two date values, divide by (60 seconds * 60 minutes * 24 hours).
COMPUTE days1 = (date2 - date1) / (60 * 60 * 24)
EXE.
*You may also use the DATEDIFF function.
COMPUTE days2 = DATEDIFF(date2,date1,"days").
EXE.
*Or even the CTIME.DAYS function.
COMPUTE days3 = CTIME.DAYS(date2 - date1).
EXE.
B. Examples of Results in Months
Examples of Results in Months.
*Subtract the two date values, divide by (60 seconds * 60 minutes * 24 hours * 365.25 days / 12 months).
COMPUTE month1 = (date2 - date1) / (60 * 60 * 24 * 365.24 / 12) .
EXE .
*You may also use the DATEDIFF function.
COMPUTE month2 = DATEDIFF(date2,date1,"month").
EXE.
C. Examples of Results in Years
Examples of Result in years (Can be used to calculate the age)
*Subtract the two date values, divide by (60 seconds * 60 minutes * 24 hours * 365.25 days)
COMPUTE year1 = (date2 - date1) / (60 * 60 * 24 * 365.24) .
EXE.
*You may also use the DATEDIFF function.
COMPUTE year2 = DATEDIFF(date2,date1,"year").
EXE.
*Another way to calculate the age using the system date.
COMPUTE age = CTIME.DAYS($TIME - date1)/365.24.
EXE.
Related Information
Historical Number
21542
Was this topic helpful?
Document Information
Modified date:
16 June 2018
UID
swg21492351