Computing interval between dates WITH times in separate variables

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

Computing interval between dates WITH times in separate variables

D.R. Wahlgren
Quick follow-up.  She has the time entered in military hours, 1600,
0030, 2200 etc.  Is there any way to convert these to time format
before proceeding with the computation below?

thanks,
Dennis


>Gang,
>A colleague has date and time variables and wants to compute the
>duration of time elapsed between two events.  I think she would like
>to display the result in hours and minutes.
>
>I know there is a DATETIME format which includes the two pieces of
>info in one variable--is there a way to compute this one variable
>from a separate date and time variable?
>
>Also, using a command like:
>
>COMPUTE hours = CTIME.HOURS(EndDateTime-StartDateTime).
>
>...can the result be formatted to display as hours and minutes?
>
>thanks,
>Dennis

=====================
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: Computing interval between dates WITH times in separate variables

Marks, Jim
Dennis:

Did you get an answer to your original question?

Are the time variables strings? or numbers formatted with leading zeros?

Here is a solution using strings for the military times.

** sample data for illustration.
data list free /startdate (date11) mil_1 (a4) enddate (date11) mil_2
(a4).
begin data
12-OCT-2005 0030 15-OCT-2005 1600
01-FEB-2008 2200 04-FEB-2008 2200
END DATA.

*** create the time variables from the strings.
comp #hour1 = number(substr(mil_1,1,2),f8.0).
comp #min1 =  number(substr(mil_1,3,2),f8.0).
compute starttime = time.hms(#hour1,#min1,0).

comp #hour2 = number(substr(mil_2,1,2),f8.0).
comp #min2 =  number(substr(mil_2,3,2),f8.0).
compute endtime = time.hms(#hour2,#min2,0).

format starttime endtime (time6.0).

** calculate the difference and formulate as hours:minutes.

compute duration =  (enddate+endtime) - (startdate + starttime)  .
format duration (time6).

exe.

Dates in SPSS have an implicit time (midnight), since dates are stored
as the number of seconds since midnight on 14 Oct 1582. Reformat a date
variable to datetime and you will see this.

Since the time format is number of seconds since midnight, you can add
them to a date.

Since you only want hour difference, the CTIME.HOURS is not needed


Since you want hours,
-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
D.R. Wahlgren
Sent: Thursday, August 07, 2008 3:37 PM
To: [hidden email]
Subject: Computing interval between dates WITH times in separate
variables

Quick follow-up.  She has the time entered in military hours, 1600,
0030, 2200 etc.  Is there any way to convert these to time format
before proceeding with the computation below?

thanks,
Dennis


>Gang,
>A colleague has date and time variables and wants to compute the
>duration of time elapsed between two events.  I think she would like
>to display the result in hours and minutes.
>
>I know there is a DATETIME format which includes the two pieces of
>info in one variable--is there a way to compute this one variable
>from a separate date and time variable?
>
>Also, using a command like:
>
>COMPUTE hours = CTIME.HOURS(EndDateTime-StartDateTime).
>
>...can the result be formatted to display as hours and minutes?
>
>thanks,
>Dennis

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