I've done a substracting of time variables (sleeptime = waketime - bedtime) and, although I get the correct result I need to categorize the sleeptime into 2 categories (sleep =0 if sleeptime => 7hours or sleep=1 if < than 7h).
The problem is that when I categorize the variable, I don't get the classification right. This is what I get:
bedtime waketime sleeptime sleep
22:00:00 07:00:00 09:00:00 1
22:30:00 06:30:00 08:00:00 1
00:55:00 08:10:00 07:15:00 0
02:30:00 08:30:00 06:00:00 1
Here's the code I've used:
data have; set want;
sleeptime = waketime - bedtime;
if sleeptime => '07:00:00't then sleep=0;
if sleeptime < '07:00:00't then sleep=1; run;
I've been think into converting the sleeptime into a value so that it's easier to categorize, for example:
bedtime waketime sleeptime sleeptime1
22:00:00 07:00:00 09:00:00 9
22:30:00 06:30:00 08:00:00 8
02:30:00 08:30:00 06:00:00 6
Any thoughts? Thanks for the help!
CodePudding user response:
Time variables are numeric, so you're fine leaving it alone... but you're forgetting about midnight!
Either keep your variables as datetime
(which keeps the date, so it lets you do this sort of thing just as you did it), or fudge it:
data have;
input bedtime :time8. waketime :time8.;
datalines;
22:00:00 07:00:00
22:30:00 06:30:00
00:55:00 08:10:00
02:30:00 08:30:00
;;;;
run;
data want;
set have;
sleeptime = waketime-bedtime (86400*(bedtime gt waketime));
format bedtime waketime sleeptime time8.;
run;
This only works if you're sure it's always going to be true that waketime
should be after bedtime
. Seems likely, but worth pointing out. (And, 86400 is the number of seconds in 24 hours - you can also use '24:00:00't
if you want.)