|
Hi,
I am trying to compute the duration in minutes between 2 times. I have 2 string variables (one corresponding to the start time and one for the end time) in the format 10:30, 9:36 etc and a second variable that goes with each string variable that designates 1 = am and 2 = pm. Does anyone know how to do this via syntax? Thanks in advance, Leah ===================== 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 |
|
Leah,
>>I am trying to compute the duration in minutes between 2 times. I have 2 string variables (one corresponding to the start time and one for the end time) in the format 10:30, 9:36 etc and a second variable that goes with each string variable that designates 1 = am and 2 = pm. do repeat x=start stop/y=tstart tstop. + compute y=number(x,time5). + If (am_pm eq 2) y=datesum(y,12,"hours"). end repeat. Compute diff=datediff(tstop,tstart,"hours"). execute. format tstart tstop(time5). Note. The difference returned is in hours. I'd expect (but do not know for sure) that minutes are expressed in fractional hours. However, the difference may be integer hours. If so, then this statement would be better (but there are other statements also). Compute diff=(tstop-tstart)/3600. Gene Maguin ===================== 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 leah rubin
Leah,
I apologize, I missed including am/pm element correctly. >>Sorry to bother you but I am not sure I am understanding your code. So I have a start and stop variable and then an am/pm variable for each. do repeat x=start stop/y=tstart tstop/z=startam_pm stopam_pm. + compute y=number(x,time5). + If (z eq 2) y=datesum(y,12,"hours"). end repeat. Compute diff=datediff(tstop,tstart,"hours"). execute. format tstart tstop(time5). >>I am trying to compute the duration in minutes between 2 times. I have 2 string variables (one corresponding to the start time and one for the end time) in the format 10:30, 9:36 etc and a second variable that goes with each string variable that designates 1 = am and 2 = pm. Gene Maguin ===================== 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 |
|
One correction - a subtle point that's often missed.
At 03:00 PM 10/21/2008, Gene Maguin wrote: >I apologize, I missed including am/pm element correctly. >[...] >do repeat x=start stop > /y=tstart tstop > /z=startam_pm stopam_pm. >+ compute y=number(x,time5). >+ If (z eq 2) y=datesum(y,12,"hours"). >end repeat. Converting am/pm times to hours is a little more subtle, because of the special status of times between 12 and 1: Times between 12:00 and 1:00 am need to be adjusted; times between 12:00 and 1:00 p.m. do not. Without looking for archived, tested code, I think this is right. It uses DO IF logic, and the older time functions. DO IF z EQ 1 /* Time in am */. . IF y GE TIME.HMS(12) /* 12:00-<1:00 a.m. */ y = y - TIME.HMS(12). ELSE IF z EQ 2 /* Time in pm */. . IF y LT TIME.HMS(12) /* 1:00 pm or after */ y = y + TIME.HMS(12). END IF. (Aside: 'tstart' and 'tstop' may well be scratch variables: '#tstart' and #tstop'. That avoids their being in the final file.) ===================== 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 leah rubin
At 12:07 PM 10/21/2008, leah rubin wrote:
>I am trying to compute the duration in minutes between 2 times. I >have 2 string variables, one corresponding to the start time and one >for the end time, in the format 10:30, 9:36 etc; and a second >variable that goes with each string variable that designates 1 = am and 2 = pm. There've been several iterations of answers, and Leah just wrote off-list that "I am having some problems getting it all to run properly", so maybe it's time for a complete solution, with test data: DATA LIST LIST / start startam_pm stop stopam_pm (A5, F2, A5, F2). <data omitted; see APPENDIX> . /**/ LIST /*-*/. List |-----------------------------|---------------------------| |Output Created |22-OCT-2008 21:20:14 | |-----------------------------|---------------------------| start startam_pm stop stopam_pm 12:25 1 1:50 1 3:35 1 4:40 1 11:30 1 12:20 2 12:10 2 1:10 2 1:30 2 2:45 2 Number of cases read: 5 Number of cases listed: 5 NUMERIC tstart tstop (TIME5). do repeat x=start stop /y=tstart tstop /z=startam_pm stopam_pm. + compute y=number(x,time5). + IF z EQ 1 /* 12:00 -< 1:00 a.m. */ AND y GE TIME.HMS(12) y = y - TIME.HMS(12). + If z eq 2 AND y LT TIME.HMS(12) /* 1:00 - 11:59 p.m. */ y = y + TIME.HMS(12). end repeat. NUMERIC Minutes (F7.2). COMPUTE Minutes = CTIME.MINUTES(tstop-tstart). LIST. |-----------------------------|---------------------------| |Output Created |22-OCT-2008 21:20:14 | |-----------------------------|---------------------------| start startam_pm stop stopam_pm tstart tstop Minutes 12:25 1 1:50 1 0:25 1:50 85.00 3:35 1 4:40 1 3:35 4:40 65.00 11:30 1 12:20 2 11:30 12:20 50.00 12:10 2 1:10 2 12:10 13:10 60.00 1:30 2 2:45 2 13:30 14:45 75.00 Number of cases read: 5 Number of cases listed: 5 ================================= APPENDIX: Test data, and all code ================================= DATA LIST LIST / start startam_pm stop stopam_pm (A5, F2, A5, F2). BEGIN DATA 12:25 1 1:50 1 3:35 1 4:40 1 11:30 1 12:20 2 12:10 2 1:10 2 1:30 2 2:45 2 END DATA. . /**/ LIST /*-*/. NUMERIC tstart tstop (TIME5). do repeat x=start stop /y=tstart tstop /z=startam_pm stopam_pm. + compute y=number(x,time5). + IF z EQ 1 /* 12:00 -< 1:00 a.m. */ AND y GE TIME.HMS(12) y = y - TIME.HMS(12). + If z eq 2 AND y LT TIME.HMS(12) /* 1:00 - 11:59 p.m. */ y = y + TIME.HMS(12). end repeat. NUMERIC Minutes (F7.2). COMPUTE Minutes = CTIME.MINUTES(tstop-tstart). LIST. ===================== 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 |
