Calculating length of time

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

Calculating length of time

Karen Harker
I have two variables in the DATE(5) (hh:mm) format (using 24-hour time) - one for time went to bed, the other for time woke up.  I would like to use a script to calculate the total time in bed in minutes rather than relying on human calculation.  I've tried DATEDIFF(), but because the times span 2 days, this doesn't calculate correctly.  Can this be done without changing the format to DATE(17) (dd/mm/yy hh:mm)?
 
Thanks.
 
 
Karen R. Harker, MLS, MPH
Biostatistical Consultant
Adolescent Mood and Addictive Disorders Research Program
UT Southwestern Medical Center
5323 Harry Hines Blvd.
Dallas, TX  75390-
214-648-5391
Yahoo IM: karenharker
Reply | Threaded
Open this post in threaded view
|

Re: Calculating length of time

parisec
Karen,
 
I just encountered this lovely issue.
 
You can use datediff () if your fields are formatted as datetime versus just time.
 
To make it simple, you can compute yourself a BeginTime and a BeginDate then just compute BeginDateTime = BeginDate+BeginTime.
 
Carol
Not a fan of PASW and will likely be another one calling it PAWS.


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Karen Harker
Sent: Wednesday, April 08, 2009 1:20 PM
To: [hidden email]
Subject: Calculating length of time

I have two variables in the DATE(5) (hh:mm) format (using 24-hour time) - one for time went to bed, the other for time woke up.  I would like to use a script to calculate the total time in bed in minutes rather than relying on human calculation.  I've tried DATEDIFF(), but because the times span 2 days, this doesn't calculate correctly.  Can this be done without changing the format to DATE(17) (dd/mm/yy hh:mm)?
 
Thanks.
 
 
Karen R. Harker, MLS, MPH
Biostatistical Consultant
Adolescent Mood and Addictive Disorders Research Program
UT Southwestern Medical Center
5323 Harry Hines Blvd.
Dallas, TX  75390-
214-648-5391
Yahoo IM: karenharker
Reply | Threaded
Open this post in threaded view
|

Re: Calculating length of time

Richard Ristow
In reply to this post by Karen Harker
At 04:20 PM 4/8/2009, Karen Harker wrote:

I have two variables in the DATE(5) (hh:mm) format (using 24-hour time) - one for time went to bed, the other for time woke up.  I would like to use a script to calculate the total time in bed in minutes rather than relying on human calculation.  I've tried DATEDIFF(), but because the times span 2 days, this doesn't calculate correctly.  Can this be done without changing the format to DATE(17) (dd/mm/yy hh:mm)?

Try this. I'm using the old functions rather than DATEDIFF, because I'm stuffy. This assumes the person goes to bed in the p.m. of one date, and awakens in the  a.m. of the following date, OR goes to bed in the same p.m. or a.m. 12-hour period, AND sleeps, overall, less than 12 hours. If those assumptions aren't correct, the logic will have to be more complex. The test data contains a case (SLOTH) for which these assumptions don't work.

Notice TIME5 instead of DATE5 format. Did you write DATE5 as a slip, or had you been using it? If you used DATE5 to read the data, that's likely a problem, too.

|-----------------------------|---------------------------|
|Output Created               |10-APR-2009 10:52:51       |
|-----------------------------|---------------------------|
WHO   ToBed Arise Comment

Early  9:12  5:30
Late  11:10  9:35
Pgmr   2:00 11:30
SLOTH  8:00 11:25 BadCase

Number of cases read:  4    Number of cases listed:  4

 
NUMERIC SleepTime (TIME5)
        SleepHrs  (F6.2).

COMPUTE SleepTime = Arise - ToBed.
IF      SleepTime LT 0
        SleepTime = TIME.HMS(12).

COMPUTE SleepHrs = CTIME.HOURS(SleepTime).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |10-APR-2009 10:52:51       |
|-----------------------------|---------------------------|
WHO   ToBed Arise Comment  SleepTime SleepHrs

Early  9:12  5:30            12:00     12.00
Late  11:10  9:35            12:00     12.00
Pgmr   2:00 11:30             9:30      9.50
SLOTH  8:00 11:25 BadCase     3:25      3.42

Number of cases read:  4    Number of cases listed:  4

=============================
APPENDIX: Test data, and code
=============================
DATA LIST LIST/
  WHO  ToBed  Arise   Comment
 (A5,   TIME5, TIME5, A8).
BEGIN DATA
 Early  09:12   5:30
 Late   11:10   9:35
 Pgmr    2:00  11:30
 SLOTH  08:00  11:25  BadCase
END DATA.
LIST.

NUMERIC SleepTime (TIME5)
        SleepHrs  (F6.2).

COMPUTE SleepTime = Arise - ToBed.
IF      SleepTime LT 0
        SleepTime = TIME.HMS(12).

COMPUTE SleepHrs = CTIME.HOURS(SleepTime).

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

Re: Calculating length of time

Karen Harker
This worked, except I still got negative values for SleepMin.  So I added an abs() function to the final compute statement: COMPUTE SleepMin = abs(CTIME.MINUTES(SleepTime)).
 
Thanks for getting me on the right track.
 
 
Karen R. Harker, MLS, MPH
Biostatistical Consultant
Adolescent Mood and Addictive Disorders Research Program
UT Southwestern Medical Center
5323 Harry Hines Blvd.
Dallas, TX  75390-
214-648-5391
Yahoo IM: karenharker


>>> On 4/10/09 at 12:12 PM, in message <[hidden email]>, Richard Ristow <[hidden email]> wrote:
Bother. I post mistakes even in code I test! See correction to the 'IF' statement that handles sleep intervals that are initially negative.

At 04:20 PM 4/8/2009, Karen Harker wrote:

I have two variables in the DATE(5) (hh:mm) format (using 24-hour time) - one for time went to bed, the other for time woke up.  I would like to calculate the total time in bed in minutes

And I'll do that, this time, instead of hours. Notice that I'm computing the difference both as an SPSS time variable, and as the number of minutes.

As before, this code assumes the person goes to bed in the p.m. of one date, and awakens in the  a.m. of the following date, OR goes to bed in the same p.m. or a.m. 12-hour period, AND sleeps, overall, less than 12 hours. The test data contains a case (SLOTH) for which these assumptions don't work. (By the way, there are warning messages when reading the data. It's loaded correctly, anyway.)

List
|-----------------------------|---------------------------|
|Output Created               |10-APR-2009 13:08:55       |
|-----------------------------|---------------------------|
WHO   ToBed Arise Comment

Early  9:12  5:30
Late  11:10  9:35
Pgmr   2:00 11:30
SLOTH  8:00 11:25 BadCase

Number of cases read:  4    Number of cases listed:  4

 
NUMERIC SleepTime (TIME5)
        SleepMin  (F6).

COMPUTE SleepTime = Arise - ToBed.
IF      SleepTime LT 0
        SleepTime = SleepTime + TIME.HMS(12).

COMPUTE SleepMin = CTIME.MINUTES(SleepTime).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |10-APR-2009 13:08:55       |
|-----------------------------|---------------------------|
WHO   ToBed Arise Comment  SleepTime SleepMin

Early  9:12  5:30             8:18       498
Late  11:10  9:35            10:25       625
Pgmr   2:00 11:30             9:30       570
SLOTH  8:00 11:25 BadCase     3:25       205

Number of cases read:  4    Number of cases listed:  4

=============================
APPENDIX: Test data, and code
=============================
DATA LIST LIST/
  WHO  ToBed  Arise   Comment
 (A5,   TIME5, TIME5, A8).
BEGIN DATA
 Early  09:12   5:30
 Late   11:10   9:35
 Pgmr    2:00  11:30
 SLOTH  08:00  11:25  BadCase
END DATA.
LIST.

NUMERIC SleepTime (TIME5)
        SleepMin  (F6).

COMPUTE SleepTime = Arise - ToBed.
IF      SleepTime LT 0
        SleepTime = SleepTime + TIME.HMS(12).

COMPUTE SleepMin = CTIME.MINUTES(SleepTime).

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

Re: Calculating length of time

Richard Ristow
At 09:56 AM 4/13/2009, Karen Harker wrote:

>This worked, except I still got negative values for SleepMin.  So I
>added an abs() function to the final compute statement:
>
>COMPUTE SleepMin = abs(CTIME.MINUTES(SleepTime)).
>
>Thanks for getting me on the right track.

You're welcome! But, to make sure there's no derailment --

ABS probably won't give you the right answer. For example, if
somebody went to bed at 11:00 and got up at 10:00, their initial
sleep time would be -60 minutes, absolute value 60 minutes, but
actual correct value is 11 hours, or 660 minutes.

Generally, when an interval looks negative, the right answer is to
add a multiple of 12 hours; my code adds exactly 12 hours. Could you
give me any start/stop times that still give you negative results?

Finally, the assumption that everyone sleeps less than 12 hours may
be shaky. Do you have any indication of am/pm with your sleep start
and stop times?

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

Re: Calculating length of time

Oliver, Richard
Minor clarification: In 24-hour clock time, going to bed at 11 and waking at 10 means you slept 23 hours.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Richard Ristow
Sent: Monday, April 13, 2009 12:07 PM
To: [hidden email]
Subject: Re: Calculating length of time

At 09:56 AM 4/13/2009, Karen Harker wrote:

>This worked, except I still got negative values for SleepMin.  So I
>added an abs() function to the final compute statement:
>
>COMPUTE SleepMin = abs(CTIME.MINUTES(SleepTime)).
>
>Thanks for getting me on the right track.

You're welcome! But, to make sure there's no derailment --

ABS probably won't give you the right answer. For example, if
somebody went to bed at 11:00 and got up at 10:00, their initial
sleep time would be -60 minutes, absolute value 60 minutes, but
actual correct value is 11 hours, or 660 minutes.

Generally, when an interval looks negative, the right answer is to
add a multiple of 12 hours; my code adds exactly 12 hours. Could you
give me any start/stop times that still give you negative results?

Finally, the assumption that everyone sleeps less than 12 hours may
be shaky. Do you have any indication of am/pm with your sleep start
and stop times?

-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

=====================
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: Calculating length of time

Richard Ristow
At 02:57 PM 4/13/2009, Oliver, Richard wrote:

Minor clarification: In 24-hour clock time, going to bed at 11 and waking at 10 means you slept 23 hours.

Quite so; and, Richard, thank you very much. I missed that the original post specified 24-hour time.

Karen, I'll bet your values that come out negative, arise because I was assuming 12-hour time.

Using 24-hour time is much better, because it requires only an assumption that nobody sleeps over 24 hours; it'll handle times over 12 hours just fine.

Here's the code, with the same test data (plus one case), but with 24-hour time.  It should be OK, unless I managed to miss something else simple.

|-----------------------------|---------------------------|
|Output Created               |13-APR-2009 19:38:19       |
|-----------------------------|---------------------------|
WHO   ToBed Arise

Early 21:12  5:30
Late  23:10  9:35
Pgmr   3:00 13:15
SLOTH 22:00 11:25
Nap   13:30 14:45

Number of cases read:  5    Number of cases listed:  5

 
NUMERIC SleepTime (TIME5)
        SleepMin  (F6).

COMPUTE SleepTime = Arise - ToBed.
IF      SleepTime LT 0
        SleepTime = SleepTime + TIME.HMS(24).

COMPUTE SleepMin = CTIME.MINUTES(SleepTime).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |13-APR-2009 19:38:20       |
|-----------------------------|---------------------------|
WHO   ToBed Arise SleepTime SleepMin

Early 21:12  5:30    8:18       498
Late  23:10  9:35   10:25       625
Pgmr   3:00 13:15   10:15       615
SLOTH 22:00 11:25   13:25       805
Nap   13:30 14:45    1:15        75

Number of cases read:  5    Number of cases listed:  5
=============================
APPENDIX: Test data, and code
=============================
DATA LIST LIST/
  WHO  ToBed  Arise
 (A5,   TIME5, TIME5).
BEGIN DATA
 Early  21:12   5:30
 Late   23:10   9:35
 Pgmr    3:00  13:15
 SLOTH  22:00  11:25
 Nap    13:30  14:45
END DATA.
LIST.

NUMERIC SleepTime (TIME5)
        SleepMin  (F6).

COMPUTE SleepTime = Arise - ToBed.
IF      SleepTime LT 0
        SleepTime = SleepTime + TIME.HMS(24).

COMPUTE SleepMin = CTIME.MINUTES(SleepTime).

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

Re: Calculating length of time

Karen Harker
That's it!  That did the trick. 
 
Thank you both for all your efforts. 
 
 
 
Karen R. Harker, MLS, MPH
Biostatistical Consultant
Adolescent Mood and Addictive Disorders Research Program
UT Southwestern Medical Center
5323 Harry Hines Blvd.
Dallas, TX  75390-
214-648-5391
Yahoo IM: karenharker


>>> On 4/13/09 at 6:46 PM, in message <[hidden email]>, Richard Ristow <[hidden email]> wrote:
At 02:57 PM 4/13/2009, Oliver, Richard wrote:

Minor clarification: In 24-hour clock time, going to bed at 11 and waking at 10 means you slept 23 hours.

Quite so; and, Richard, thank you very much. I missed that the original post specified 24-hour time.

Karen, I'll bet your values that come out negative, arise because I was assuming 12-hour time.

Using 24-hour time is much better, because it requires only an assumption that nobody sleeps over 24 hours; it'll handle times over 12 hours just fine.

Here's the code, with the same test data (plus one case), but with 24-hour time.  It should be OK, unless I managed to miss something else simple.

|-----------------------------|---------------------------|
|Output Created               |13-APR-2009 19:38:19       |
|-----------------------------|---------------------------|
WHO   ToBed Arise

Early 21:12  5:30
Late  23:10  9:35
Pgmr   3:00 13:15
SLOTH 22:00 11:25
Nap   13:30 14:45

Number of cases read:  5    Number of cases listed:  5

 
NUMERIC SleepTime (TIME5)
        SleepMin  (F6).

COMPUTE SleepTime = Arise - ToBed.
IF      SleepTime LT 0
        SleepTime = SleepTime + TIME.HMS(24).

COMPUTE SleepMin = CTIME.MINUTES(SleepTime).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |13-APR-2009 19:38:20       |
|-----------------------------|---------------------------|
WHO   ToBed Arise SleepTime SleepMin

Early 21:12  5:30    8:18       498
Late  23:10  9:35   10:25       625
Pgmr   3:00 13:15   10:15       615
SLOTH 22:00 11:25   13:25       805
Nap   13:30 14:45    1:15        75

Number of cases read:  5    Number of cases listed:  5
=============================
APPENDIX: Test data, and code
=============================
DATA LIST LIST/
  WHO  ToBed  Arise
 (A5,   TIME5, TIME5).
BEGIN DATA
 Early  21:12   5:30
 Late   23:10   9:35
 Pgmr    3:00  13:15
 SLOTH  22:00  11:25
 Nap    13:30  14:45
END DATA.
LIST.

NUMERIC SleepTime (TIME5)
        SleepMin  (F6).

COMPUTE SleepTime = Arise - ToBed.
IF      SleepTime LT 0
        SleepTime = SleepTime + TIME.HMS(24).

COMPUTE SleepMin = CTIME.MINUTES(SleepTime).

LIST.

Reply | Threaded
Open this post in threaded view
|

correlation for quadratic / curved data?

Cho, Stanley
In reply to this post by Richard Ristow
Hello all,

I have data for seasonal disease trends, monitored by two different
surveillance systems.  As expected, the frequency of disease rises and
falls during the year.

I'm trying to do a correlation coefficient between these two variables,
but a strictly linear correlation is not entirely appropriate.  Since
there's only one curve, I think the trend can best be described as
quadratic.  Can anybody let me know how to do a correlation for that
kind of data?  Or is the linear correlation appropriate after all?

Example (very simplified):

Week / Sys1 / Sys2
1 / 10 / 0
2 / 12 / 0
3 / 15 / 2
4 / 18 / 2
5 / 24 / 4
6 / 33 / 7
7 / 40 / 11
8 / 42 / 12
9 / 40 / 10
10 / 38 / 9
11 / 36 / 8
12 / 28 / 6
13 / 19 / 3
14 / 14 / 3
15 / 11 / 2
16 / 10 / 1

Sincerely,
--Stan

=====================
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: correlation for quadratic / curved data?

SR Millis-3
Spearman correlation coefficient expresses monotonic
relationships that aren't linear.

Scott Millis

=====================
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: correlation for quadratic / curved data?

Burleson,Joseph A.
In reply to this post by Cho, Stanley
Square one of the variables and see if it the correlates with the
original of the other.

Ideally, one should center the predictor variable, the square it as a
separate variable.

Then, in a multiple regression, enter the unsquared variable as the DV,
then the unsquared (centered) predictor variable, then the squared
(centered) predictor variable. Request the SEQUENTIAL option (Type I) if
GLM, or enter them in separate /METHOD subcommands if the old
REGRESSION.

This should give you both the linear correlation, and that proportion of
the variance accounted for by the curvilinear effect, AFTER the effect
of the linear is partialled out.

If you truly want to ignore any effects of the linear correlation, just
put in the squared predictor.

Joe Burleson

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Cho, Stanley
Sent: Tuesday, April 14, 2009 12:22 PM
To: [hidden email]
Subject: correlation for quadratic / curved data?

Hello all,

I have data for seasonal disease trends, monitored by two different
surveillance systems.  As expected, the frequency of disease rises and
falls during the year.

I'm trying to do a correlation coefficient between these two variables,
but a strictly linear correlation is not entirely appropriate.  Since
there's only one curve, I think the trend can best be described as
quadratic.  Can anybody let me know how to do a correlation for that
kind of data?  Or is the linear correlation appropriate after all?

Example (very simplified):

Week / Sys1 / Sys2
1 / 10 / 0
2 / 12 / 0
3 / 15 / 2
4 / 18 / 2
5 / 24 / 4
6 / 33 / 7
7 / 40 / 11
8 / 42 / 12
9 / 40 / 10
10 / 38 / 9
11 / 36 / 8
12 / 28 / 6
13 / 19 / 3
14 / 14 / 3
15 / 11 / 2
16 / 10 / 1

Sincerely,
--Stan

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