# Functions for calculating time between 2 variables

#### Laf

##### New Member
Hello,

I have a large data set, and, in the data set, there are a number of dates entered as string variables (e.g., 05SEP2012).

I need to determine the length of time in years between some of these variables (e.g., from 20MAR1969 to 05SEP2012), but can't seem to figure it out. I have tried a few things under the compute variable functions and date and time wizard but haven't had success. Data set way too large to calculate manually.

Any ideas/suggestions?

Thanks,
L

#### helicon

##### Member
Here you go (assumes first date variable is named 'date1' and second is 'date2'):

Code:
str #date1 #date2 (a11).
comp #date1=concat(substr(date1,1,2),"/",substr(date1,3,3),"/",substr(date1,6,4)).
comp #date2=concat(substr(date2,1,2),"/",substr(date2,3,3),"/",substr(date2,6,4)).
comp newdate1=number(#date1,date11).
comp newdate2=number(#date2,date11).
formats newdate1 newdate2 (date11).
comp difference=datediff(newdate2, newdate1, "years").
exe.