# Loop for calculating absolute date differences in days

7 messages
Open this post in threaded view
|

## Loop for calculating absolute date differences in days

 Hello, I have searched the listings and been able to solve the second part of my problem but need help with the first part. I have a data set that attempts to project dates for attendance, and compare this to actual attendance dates. The final variable should tell me the absolute date difference (in days) between my 10 simulated attendance dates, and the closest of 10 actual attendance dates. I have been able to calculate the absolute date difference manually: COMPUTE  Absolute1Diff1  = RND((SimStart1 - TrueStart1) / time.days(1)). COMPUTE  Absolute1Diff2  = RND((SimStart1 - TrueStart2) / time.days(1)). COMPUTE  Absolute1Diff3  = RND((SimStart1 - TrueStart3) / time.days(1)). COMPUTE  Absolute1Diff4  = RND((SimStart1 - TrueStart4) / time.days(1)). IF (Absolute1Diff1 LT 0) AbsoluteDiff1 = AbsoluteDiff1 * (-1) . IF (Absolute1Diff2 LT 0) AbsoluteDiff2 = AbsoluteDiff2 * (-1) . Etc. EXECUTE . My dataset looks like this: SimStart1       TrueStart1        TrueStart2      TrueStart3        TrueStart4 10-JAN-1999    12-FEB-1999   14-FEB-1999   20-MAR-1999    01-JAN-2000 I have been unable to write a loop that will correctly work though the TrueStart dates to calculate the absolute date difference between the SimStart1 compared to TrueStart1 to Truestart10 and do the same for SimStart2 etc. though I the syntax below works to search through and find the smallest and largest absolute date difference: DO REPEAT OLD=Absolute1Diff1 TO  Absolute1Diff4 / cpy=#x1 to #x4. +  COMPUTE cpy=old. END REPEAT. COMPUTE #XMIN=MIN(Absolute1Diff1 TO  Absolute1Diff4). COMPUTE #XMAX=MAX(Absolute1Diff1 TO  Absolute1Diff4). VECTOR #X=#X1 TO #x4 . DO REPEAT FX=#XMIN #XMAX . +  COMPUTE #Found=0. +    LOOP #=1 TO 4 . +      DO IF #X(#)=FX. +        COMPUTE #Found=1. +      END IF. +  END LOOP IF #found. END REPEAT. COMPUTE MinAbsDiff1=MIN(#x1 TO #X4). COMPUTE MaxfEoC1=MAX(#x1 TO #X4). EXECUTE . I would be grateful for any insight. Thank you, Gesine
Open this post in threaded view
|

## Automatic reply: Loop for calculating absolute date differences in days

 I will be off campus attending the meeting of the Society for Medical Decision Making in Phoenix this week and will not be checking email. I will return for classes Monday the 22nd.
Open this post in threaded view
|

## Re: Loop for calculating absolute date differences in days

 In reply to this post by morticia If these are true SPSS date variables, use the datediff function with COMPUTE. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] new phone: 720-342-5621 From:        morticia <[hidden email]> To:        [hidden email], Date:        10/18/2012 09:55 AM Subject:        [SPSSX-L] Loop for calculating absolute date differences in days Sent by:        "SPSSX(r) Discussion" <[hidden email]> Hello, I have searched the listings and been able to solve the second part of my problem but need help with the first part. I have a data set that attempts to project dates for attendance, and compare this to actual attendance dates. The final variable should tell me the absolute date difference (in days) between my 10 simulated attendance dates, and the closest of 10 actual attendance dates. I have been able to calculate the absolute date difference manually: COMPUTE  Absolute1Diff1  = RND((SimStart1 - TrueStart1) / time.days(1)). COMPUTE  Absolute1Diff2  = RND((SimStart1 - TrueStart2) / time.days(1)). COMPUTE  Absolute1Diff3  = RND((SimStart1 - TrueStart3) / time.days(1)). COMPUTE  Absolute1Diff4  = RND((SimStart1 - TrueStart4) / time.days(1)). IF (Absolute1Diff1 LT 0) AbsoluteDiff1 = AbsoluteDiff1 * (-1) . IF (Absolute1Diff2 LT 0) AbsoluteDiff2 = AbsoluteDiff2 * (-1) . Etc. EXECUTE . My dataset looks like this: SimStart1       TrueStart1        TrueStart2      TrueStart3 TrueStart4 10-JAN-1999    12-FEB-1999   14-FEB-1999   20-MAR-1999    01-JAN-2000 I have been unable to write a loop that will correctly work though the TrueStart dates to calculate the absolute date difference between the SimStart1 compared to TrueStart1 to Truestart10 and do the same for SimStart2 etc. though I the syntax below works to search through and find the smallest and largest absolute date difference: DO REPEAT OLD=Absolute1Diff1 TO  Absolute1Diff4 / cpy=#x1 to #x4. +  COMPUTE cpy=old. END REPEAT. COMPUTE #XMIN=MIN(Absolute1Diff1 TO  Absolute1Diff4). COMPUTE #XMAX=MAX(Absolute1Diff1 TO  Absolute1Diff4). VECTOR #X=#X1 TO #x4 . DO REPEAT FX=#XMIN #XMAX . +  COMPUTE #Found=0. +    LOOP #=1 TO 4 . +      DO IF #X(#)=FX. +        COMPUTE #Found=1. +      END IF. +  END LOOP IF #found. END REPEAT. COMPUTE MinAbsDiff1=MIN(#x1 TO #X4). COMPUTE MaxfEoC1=MAX(#x1 TO #X4). EXECUTE . I would be grateful for any insight. Thank you, Gesine -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Loop-for-calculating-absolute-date-differences-in-days-tp5715727.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
Open this post in threaded view
|

## Re: Loop for calculating absolute date differences in days

 Jon K Peck wrote If these are true SPSS date variables, use the datediff function with COMPUTE. Perfect - I now have: COMPUTE Absolute1Diff1 = datediff(SimStart1, TrueStart1, "days") . COMPUTE Absolute1Diff2 = datediff(SimStart1, TrueStart2, "days") . COMPUTE Absolute1Diff3 = datediff(SimStart1, TrueStart3, "days") . COMPUTE Absolute1Diff4 = datediff(SimStart1, TrueStart4, "days") . EXECUTE . When I try using a loop, my logic does not work. This is what I have so far: VECTOR Absolute1Diff(4) / ACTUAL=TrueStart1 TO TrueStart4 . LOOP #i = 1 TO 4 . COMPUTE #j= ACTUAL(#i). COMPUTE Absolute1Diff(i) = datediff(SimStart1,( #j), "days") . END LOOP . EXECUTE .
Open this post in threaded view
|