Date: Wed, 30 Jan 2008 14:14:53 -0500
Reply-To: Richard Ristow <wrristow@mindspring.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Richard Ristow <wrristow@mindspring.com>
Subject: Re: date arithmetic
In-Reply-To: <15186109.post@talk.nabble.com>
Content-Type: text/plain; charset="us-ascii"; format=flowed
At 12:15 PM 1/30/2008, jimjohn wrote:
>i have two numeric variables with dates in the following format
>yyyymmdd, one of them is always greater than equal to the other
>
>i want to get rid of all my records for which there is less than 8
>months difference between these two variables.
>
>i know of a datediff function, but my variable has to be a string
>for that? is there a way to maybe just convert them to strings and
>then use datediff? thanks.
For DATEDIFF, the variables must be SPSS date values. SPSS date
values are not strings; they are numeric, in a special
representation(*). You can often read input dates as SPSS date
values. Or, if you already have dates in other formats, you convert
them to SPSS date values by first splitting them into parts, then
using the SPSS 'DATE.' functions. There've been posted solutions for
just your format, but here goes. I'm using DO REPEAT logic, so as to
write the code only once; and then, DATEDIFF on the result:
|-----------------------------|---------------------------|
|Output Created |30-JAN-2008 14:12:20 |
|-----------------------------|---------------------------|
Date1 Date2
20010704 20010930
20030214 20031225
Number of cases read: 2 Number of cases listed: 2
DO REPEAT InptDate = Date1 Date2
/SPSSdate = SPSS_Dt1 SPSS_Dt2.
. NUMERIC SPSSdate (DATE11).
. COMPUTE #Day = MOD( InptDate,100).
. COMPUTE #Month = MOD((InptDate-#Day)/100,100).
. COMPUTE #Year = (InptDate-100*#Month-#Day)/1E4.
. COMPUTE SPSSdate = DATE.MDY(#Month,#Day,#Year).
END REPEAT.
NUMERIC MnthDiff (F3).
COMPUTE MnthDiff=DATEDIFF(SPSS_Dt2,SPSS_Dt1,"Months").
LIST.
List
|-----------------------------|---------------------------|
|Output Created |30-JAN-2008 14:12:20 |
|-----------------------------|---------------------------|
Date1 Date2 SPSS_Dt1 SPSS_Dt2 MnthDiff
20010704 20010930 04-JUL-2001 30-SEP-2001 2
20030214 20031225 14-FEB-2003 25-DEC-2003 10
Number of cases read: 2 Number of cases listed: 2
................................
TEST DATA for conversion example:
................................
DATA LIST FREE /Date1 Date2 (2F8).
BEGIN DATA.
20010704 20010930 20030214 20031225
END DATA.
LIST.
.................................
(*) SPSS date values: From SPSS 14 Command Syntax Reference, p.72:
>.. A date is a floating-point number representing the number of
>seconds from midnight, October 14, 1582. Dates, which represent a
>particular point in time, are stored as the number of seconds to
>that date. For example, November 8, 1957, is stored as [11,836,281,600].
>.. A date includes the time of day, which is the time interval past
>midnight. When time of day is not given, it is taken as 00:00 and
>the date is an even multiple of 86,400 (the number of seconds in a day).
.................
Demonstration:
DATA LIST FREE /TestDate (DATE11).
BEGIN DATA
11-Nov-1957
END DATA.
NUMERIC DateNmbr (COMMA15).
COMPUTE DateNmbr=TestDate.
LIST.
List
|-----------------------------|---------------------------|
|Output Created |30-JAN-2008 13:13:45 |
|-----------------------------|---------------------------|
TestDate DateNmbr
11-NOV-1957 11,836,281,600
Number of cases read: 1 Number of cases listed: 1
=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (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