Loop for calculating absolute date differences in days

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

Loop for calculating absolute date differences in days

morticia
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
Reply | Threaded
Open this post in threaded view
|

Automatic reply: Loop for calculating absolute date differences in days

Beckstead, Jason

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.

 

Reply | Threaded
Open this post in threaded view
|

Re: Loop for calculating absolute date differences in days

Jon K Peck
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


Reply | Threaded
Open this post in threaded view
|

Re: Loop for calculating absolute date differences in days

morticia
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 .
Reply | Threaded
Open this post in threaded view
|

Re: Loop for calculating absolute date differences in days

Maguin, Eugene
In reply to this post by morticia
The first code segment could be condensed using DateDiff as Jon suggested.

Do repeat ts= TrueStart1 TrueStart2 TrueStart3 TrueStart4/
   Ad= Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4.
+   compute ad=abs(datediff(ts, SimStart1,"days")).  /*  you want minimum absolute value? Correct?.
End repeat.
Compute admin=min(Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4).
Compute admax=max(Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4).


I don't understand whether you want to compute the min and max difference between all combinations of SimStart1 thru 4 with TrueStart1 thru 4 or Simstart1 with Truestart1, etc, but it seems to be the former.

Vector ss= SimStart1 to SimStart4.
Loop #i=1 to 4.
Do repeat ts= TrueStart1 TrueStart2 TrueStart3 TrueStart4/ad = #ad1 #ad2 #ad3 #ad4.
+   compute ad=abs(datediff(ts, ss(#i),"days")).
End repeat.
Compute admin=min(admin, #ad1, #ad2, #ad3, #ad4).
Compute admax=max(admax, #ad1, #ad2, #ad3, #ad4).
End loop.

On the other hand, if you want the latter. Then,

Do repeat ts= TrueStart1 TrueStart2 TrueStart3 TrueStart4/
   Ss= SimStart1 SimStart2 SimStart3 SimStart4/ ad = #ad1 #ad2 #ad3 #ad4.
+   compute ad=abs(datediff(ts, SS,"days")).
End repeat.
Compute admin=min(admin, #ad1, #ad2, #ad3, #ad4).
Compute admax=max(admax, #ad1, #ad2, #ad3, #ad4).




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of morticia
Sent: Thursday, October 18, 2012 11:52 AM
To: [hidden email]
Subject: 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



--
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

=====================
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: Loop for calculating absolute date differences in days

morticia
Yes, this is exactly what I have been trying to figure out.
Thank you!

Maguin, Eugene wrote
The first code segment could be condensed using DateDiff as Jon suggested.

Do repeat ts= TrueStart1 TrueStart2 TrueStart3 TrueStart4/
   Ad= Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4.
+   compute ad=abs(datediff(ts, SimStart1,"days")).  /*  you want minimum absolute value? Correct?.
End repeat.
Compute admin=min(Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4).
Compute admax=max(Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4).


I don't understand whether you want to compute the min and max difference between all combinations of SimStart1 thru 4 with TrueStart1 thru 4 or Simstart1 with Truestart1, etc, but it seems to be the former.
Reply | Threaded
Open this post in threaded view
|

Re: Loop for calculating absolute date differences in days

David Marso
Administrator
This post was updated on .
In reply to this post by Maguin, Eugene
Vector ss= SimStart1 to SimStart4
     / ts= TrueStart1 TrueStart2 TrueStart3 TrueStart4.
LOOP #I=1 TO 4.
+  LOOP #J=1 TO 4.
+    COMPUTE #ad=ABS(ss(#I)-ts(#J)).
+    COMPUTE admax=MAX(admax,#ad).
+    COMPUTE admin=MIN(admin,#ad).
+  END LOOP.
END LOOP.
COMPUTE maxdays=CTIME.DAYS(admax).
COMPUTE mindays=CTIME.DAYS(admin).

Maguin, Eugene wrote
The first code segment could be condensed using DateDiff as Jon suggested.

Do repeat ts= TrueStart1 TrueStart2 TrueStart3 TrueStart4/
   Ad= Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4.
+   compute ad=abs(datediff(ts, SimStart1,"days")).  /*  you want minimum absolute value? Correct?.
End repeat.
Compute admin=min(Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4).
Compute admax=max(Absolute1Diff1 Absolute1Diff2 Absolute1Diff3 Absolute1Diff4).


I don't understand whether you want to compute the min and max difference between all combinations of SimStart1 thru 4 with TrueStart1 thru 4 or Simstart1 with Truestart1, etc, but it seems to be the former.

Vector ss= SimStart1 to SimStart4.
Loop #i=1 to 4.
Do repeat ts= TrueStart1 TrueStart2 TrueStart3 TrueStart4/ad = #ad1 #ad2 #ad3 #ad4.
+   compute ad=abs(datediff(ts, ss(#i),"days")).
End repeat.
Compute admin=min(admin, #ad1, #ad2, #ad3, #ad4).
Compute admax=max(admax, #ad1, #ad2, #ad3, #ad4).
End loop.

On the other hand, if you want the latter. Then,

Do repeat ts= TrueStart1 TrueStart2 TrueStart3 TrueStart4/
   Ss= SimStart1 SimStart2 SimStart3 SimStart4/ ad = #ad1 #ad2 #ad3 #ad4.
+   compute ad=abs(datediff(ts, SS,"days")).
End repeat.
Compute admin=min(admin, #ad1, #ad2, #ad3, #ad4).
Compute admax=max(admax, #ad1, #ad2, #ad3, #ad4).




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of morticia
Sent: Thursday, October 18, 2012 11:52 AM
To: [hidden email]
Subject: 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



--
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

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"