|
hi guys 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. does anyone know how to do this in spss? 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. |
|
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 [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 |
|
In reply to this post by jimjohn
Convert them to dates, then use the datediff function. Converting numeric "dates" of the form yyyymmdd to SPSS dates was discussed recently. Here's one way:
compute #day=mod(datevar,100). compute #year=trunc(datevar/10000). compute #month=trunc(mod(datevar,10000)/100). compute newdate=date.mdy(#month, #day, #year). formats newdate (adate10). -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of jimjohn Sent: Wednesday, January 30, 2008 11:16 AM To: [hidden email] Subject: date arithmetic hi guys 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. does anyone know how to do this in spss? 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. -- View this message in context: http://www.nabble.com/date-arithmetic-tp15186109p15186109.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== 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 |
|
In reply to this post by jimjohn
Hi,
Here is a SPSS 16 tested syntax: /*** first let's have some test data **/ DATA LIST LIST /DATE1 DATE2 (2N8). BEGIN DATA 20060827 20070717 20080130 20070104 20080130 20071105 END DATA. /*** i first transform the numeric var into string and then compute date variables from the string ***/ STRING #DATE1S #DATE2S (A8). COMPUTE #DATE1S=STRING(DATE1,N8). COMPUTE #DATE2S=STRING(DATE2,N8). COMPUTE DATE1D = date.dmy(number(substr(ltrim(#DATE1S),7,2),f2.0), number(substr(ltrim(#DATE1S),5,2),f2.0), number(substr(ltrim(#DATE1S),1,4),f4.0)). COMPUTE DATE2D = date.dmy(number(substr(ltrim(#DATE2S),7,2),f2.0), number(substr(ltrim(#DATE2S),5,2),f2.0), number(substr(ltrim(#DATE2S),1,4),f4.0)). VARIABLE LEVEL DATE1D (SCALE). FORMATS DATE1D (ADATE10). VARIABLE WIDTH DATE1D(10). VARIABLE LEVEL DATE2D (SCALE). FORMATS DATE2D (ADATE10). VARIABLE WIDTH DATE2D(10). /*** now let's compute the difference between the new variables ***/ COMPUTE DIFFM = DATEDIF(DATE1D, DATE2D, "months"). VARIABLE LABEL DIFFM. VARIABLE LEVEL DIFFM (SCALE). FORMATS DIFFM (F5.0). VARIABLE WIDTH DIFFM(5). /*** and finally select the cases you need ***/ SELECT IF ABS(DIFFM)<8. EXE. HTH, Luca Mr. Luca MEYER Market research, data analysis & more www.lucameyer.com - Tel: +39.339.495.00.21 -----Messaggio originale----- Da: SPSSX(r) Discussion [mailto:[hidden email]] Per conto di jimjohn Inviato: mercoledì 30 gennaio 2008 18.16 A: [hidden email] Oggetto: date arithmetic hi guys 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. does anyone know how to do this in spss? 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. -- View this message in context: http://www.nabble.com/date-arithmetic-tp15186109p15186109.html Sent from the SPSSX Discussion mailing list archive at Nabble.com. ===================== 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 No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.516 / Virus Database: 269.19.14/1247 - Release Date: 28/01/2008 10.59 No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.516 / Virus Database: 269.19.14/1247 - Release Date: 28/01/2008 10.59 ===================== 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 |
| Free forum by Nabble | Edit this page |
