Date difference

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Date difference

Vicent Giner-Bosch
Hi!
 
Some years ago I asked about a way in SPSS to compute the difference between
two dates, in months, and I got some interesting answers.
 
Now, I come back to SPSS, version 14, and I realize that, now, there IS a
function called "DATEDIFF", which computes months (or days, or years, etc.)
between two given dates. But it returns an integer number —I mean, it
truncates the result.
 
I would like to ask if there is a way to "imitate" the behaviour of the
function "MONTHS_BETWEEN" in Oracle PL/SQL language, which computes the
number of months between 2 dates as a real number, not integer.
 
The procedure followed by this function is described in the section  12.1.3
of this web page: http://www.unix.org.ua/orelly/oracle/prog2/ch12_01.htm .
Also some examples are given.
 
Moreover, there exists something similar in Excel —DATEDIF and YEARFRAC
functions (http://office.microsoft.com/en-us/excel/HP052093441033.aspx); in
Spanish, FRAC.AÑO
(http://office.microsoft.com/es-es/excel/HP052093443082.aspx).
 
So, any function or procedure which imitates those functions in SPSS?
 
Thank you in advance.
     
--
Vicent

====================To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Date difference

Peck, Jon
There is an inherent problem with fractional parts of a calendar unit like months where the size of the unit varies.  What do you mean by the fraction?

If you use the Date/Time wizard, it now offers three choices for the datediff calculation.  (This enhancement postdates SPSS 14, I think, but it illustrates the issue).

You can truncate the difference or round it to an integer.  But you can also now retain a fractional part.  In that case, for the variable length units of month and year, the computation is based on the average unit length, which is 365.25 days for years and 30.4375 for months.

This would be done in syntax (in any SPSS version) as
compute diffinyears = (date2 - date1)/(365.25 * time.days(1)).
or
compute diffinmonths = (date2 - date1)/(30.4375 * time.days(1)).

For rounding or truncation for exact calendar units, the DATEDIF function is used.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Vicent Giner Bosch
Sent: Monday, December 24, 2007 6:18 AM
To: [hidden email]
Subject: [SPSSX-L] Date difference

Hi!

Some years ago I asked about a way in SPSS to compute the difference between
two dates, in months, and I got some interesting answers.

Now, I come back to SPSS, version 14, and I realize that, now, there IS a
function called "DATEDIFF", which computes months (or days, or years, etc.)
between two given dates. But it returns an integer number —I mean, it
truncates the result.

I would like to ask if there is a way to "imitate" the behaviour of the
function "MONTHS_BETWEEN" in Oracle PL/SQL language, which computes the
number of months between 2 dates as a real number, not integer.

The procedure followed by this function is described in the section  12.1.3
of this web page: http://www.unix.org.ua/orelly/oracle/prog2/ch12_01.htm .
Also some examples are given.

Moreover, there exists something similar in Excel —DATEDIF and YEARFRAC
functions (http://office.microsoft.com/en-us/excel/HP052093441033.aspx); in
Spanish, FRAC.AÑO
(http://office.microsoft.com/es-es/excel/HP052093443082.aspx).

So, any function or procedure which imitates those functions in SPSS?

Thank you in advance.

--
Vicent

=======
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD