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

Richard Ristow
At 01:41 PM 8/7/2008, D.R. Wahlgren wrote:

>A colleague has date and time variables and wants to compute the
>duration of time elapsed between two events. 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?

Sure. All SPSS 'date' variables are actually date-time variables; a
'date' is the midnight that begins the day, i.e. its time portion is 0.

If you have variables DATE1, TIME1, DATE2, TIME2, *and they are SPSS
date and time variables* (see below), the following (untested) should work:

NUMERIC DT_TIME1 DT_TIME2 (DATETIME17) /* Not strictly needed */.
COMPUTE DT_TIME1 = DATE1 + TIME1.
COMPUTE DT_TIME2 = DATE2 + TIME2.

>...can the result be formatted to display as hours and minutes?

Yes, but only if the result is an SPSS time value. The result of
function CTIME.HOURS is not an SPSS time value; SPSS time values are
in seconds.

NUMERIC DURATION(TIME5).
COMPUTE DURATION = DT_TIME2 - DT_TIME1.

That should display as she wants. If you use this DURATION in
computations, remember that it is in *seconds*.

>She has the time entered in military hours, 1600, 0030, 2200
>etc.  Is there any way to convert these to time format?

Sure. If they're numeric, then
NUMERIC TIME1(TIME5)  /* Not strictly necessary */.

COMPUTE #HOURS   = TRUNC(MILTIME1/100).
COMPUTE #MINUTES = MOD  (MILTIME1,100).

COMPUTE #TIME1   = TIME.HMS(#HOURS,#MINUTES).

and the same for TIME2, of course.  (You may want to use a DO REPEAT.)


If they're 4-character strings, then
NUMERIC TIME1(TIME5)  /* Not strictly necessary */.

COMPUTE #HOURS   = NUMERIC(SUBSTR(MILTIME1,1,2),F2).
COMPUTE #MINUTES = NUMERIC(SUBSTR(MILTIME1,3,2),F2).

COMPUTE #TIME1   = TIME.HMS(#HOURS,#MINUTES).

-Best of luck,
  Richard

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