Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting time variable into a numeric value in SAS

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading