Computing a duration variable

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

Computing a duration variable

danirockyo
I'm relatively new to using syntax in SPSS and I'm struggling with computing a duration variable. I have traffic stop data and I need to determine how long the stop was in minutes. When I simply subtract end time from the start time, I get the length. This is fine for most cases. However, about 300 of my traffic stops happen late at night and carry on to the next day. Here, I'm getting negative values. I have all date and time variables. Any suggestions?

Thanks in advance, I know this is a simple question.

danirockyo
Reply | Threaded
Open this post in threaded view
|

Re: Computing a duration variable

John F Hall
Not my speciality, but. with Data Editor open in Variable View, and Type
declared as Date, click on Date to get a list of date/time formats
available.  These include yy-mm-dd-hh-ss formats from which you should be
able to calculate intervals in minutes:seconds.

In the fine manual [Help > Command syntax ref] scroll down to the DATE
command in the menu (pp 529 ff)

John F Hall (Mr)
[Retired academic survey researcher]

Email:   [hidden email]  
Website: www.surveyresearch.weebly.com
SPSS start page:  www.surveyresearch.weebly.com/1-survey-analysis-workshop




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
danirockyo
Sent: 15 January 2016 06:24
To: [hidden email]
Subject: Computing a duration variable

I'm relatively new to using syntax in SPSS and I'm struggling with computing
a duration variable. I have traffic stop data and I need to determine how
long the stop was in minutes. When I simply subtract end time from the start
time, I get the length. This is fine for most cases. However, about 300 of
my traffic stops happen late at night and carry on to the next day. Here,
I'm getting negative values. I have all date and time variables. Any
suggestions?

Thanks in advance, I know this is a simple question.

danirockyo



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Computing-a-duration-variable-
tp5731288.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: Computing a duration variable

Andy W
In reply to this post by danirockyo
You have to add the date and the time together. (Which surely you have.)

So something like:

COMPUTE TimeDif = (EndDate + EndTime) - (BeginDate + BeginTime).

This will return the time difference in seconds - so if you want minutes, you can divide by 60.

You can use the DATEDIFF transformation command to the same effect, so:

COMPUTE TimeDif = DATEDIFF(EndDate + EndTime,BeginDate + BeginTime,"MINUTES").
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Computing a duration variable

Richard Ristow
In reply to this post by danirockyo
At 12:23 AM 1/15/2016, danirockyo wrote:

>I have traffic stop data and I need to determine how long the stop
>was in minutes. When I simply subtract end time from the start time,
>I get the length. This is fine for most cases. However, about 300 of
>my traffic stops happen late at night and carry on to the next day.
>Here, I'm getting negative values. I have all date and time variables.

It sounds like you have the dates and times separately. If you have
two SPSS time values; you want the time between them but they may
fall in different days; then, IF the interval can never be more than
24 hours, it's simple:

COMPUTE @TimeDiff = @EndTime  - @StartTime /* as you've done */.
IF     (@TimeDiff LT 0)
         @TimeDiff = @TimeDiff + TIME.HMS(24).

Variable @TimeDiff wil be an SPSS time value; assign it an appropriate format.

=====================
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 a duration variable

PRogman
In reply to this post by danirockyo
If you know that the measured time periods never exceed  24 hours (86400 seconds) you can use

COMPUTE dTime =  CTIMES.MINUTES(MOD((Time2  - Time1 + 86400), 86400)).

/PRogman

danirockyo wrote
I'm relatively new to using syntax in SPSS and I'm struggling with computing a duration variable. I have traffic stop data and I need to determine how long the stop was in minutes. When I simply subtract end time from the start time, I get the length. This is fine for most cases. However, about 300 of my traffic stops happen late at night and carry on to the next day. Here, I'm getting negative values. I have all date and time variables. Any suggestions?

Thanks in advance, I know this is a simple question.

danirockyo