BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lavdiff
Obsidian | Level 7

Having issues subtracting military time from two variables

 

variable one is the time people woke up (wokeup)

 variable two is the time people went to sleep (sleep)

 

I want to find the amount of hours they subjects sleep.

 

I created a third variable hours where i substracted noth variables 

 

and the results are not giving the hours.

 

can you please point me to the right direction...

 

 

this the data

 

IF H4SP1H = 1 AND H4SP1T =1 THEN WHOUR24 = 0100;
IF H4SP1H = 2 AND H4SP1T =1 THEN WHOUR24 = 0200;
IF H4SP1H = 3 AND H4SP1T =1 THEN WHOUR24 = 0300;
IF H4SP1H = 4 AND H4SP1T =1 THEN WHOUR24 = 0400;
IF H4SP1H = 5 AND H4SP1T =1 THEN WHOUR24 = 0500;
IF H4SP1H = 6 AND H4SP1T =1 THEN WHOUR24 = 0600;
IF H4SP1H = 7 AND H4SP1T =1 THEN WHOUR24 = 0700;
IF H4SP1H = 8 AND H4SP1T =1 THEN WHOUR24 = 0800;
IF H4SP1H = 9 AND H4SP1T =1 THEN WHOUR24 = 0900;
IF H4SP1H = 10 AND H4SP1T =1 THEN WHOUR24 = 1000;
IF H4SP1H = 11 AND H4SP1T =1 THEN WHOUR24 = 1100;
IF H4SP1H = 12 AND H4SP1T =1 THEN WHOUR24 = 0000;
IF H4SP1H = 1 AND H4SP1T =2 THEN WHOUR24 = 1300;
IF H4SP1H = 2 AND H4SP1T =2 THEN WHOUR24 = 1400;
IF H4SP1H = 3 AND H4SP1T =2 THEN wHOUR24 = 1500;
IF H4SP1H = 4 AND H4SP1T =2 THEN WHOUR24 = 1600;
IF H4SP1H = 5 AND H4SP1T =2 THEN WHOUR24 = 1700;
IF H4SP1H = 6 AND H4SP1T =2 THEN WHOUR24 = 1800;
IF H4SP1H = 7 AND H4SP1T =2 THEN WHOUR24 = 1900;
IF H4SP1H = 8 AND H4SP1T =2 THEN WHOUR24 = 2000;
IF H4SP1H = 9 AND H4SP1T =2 THEN WHOUR24 = 2100;
IF H4SP1H = 10 AND H4SP1T =2 THEN WHOUR24 = 2200;
IF H4SP1H = 11 AND H4SP1T =2 THEN wHOUR24 = 2300;
IF H4SP1H = 12 AND H4SP1T =2 THEN wHOUR24 = 1200;
WoScWake=SUM(of H4SP1M WHOUR24); /*work or school next day*/
IF H4SP2H = 1 AND H4SP2T =1 THEN HOUR24 = 0100;
IF H4SP2H = 2 AND H4SP2T =1 THEN HOUR24 = 0200;
IF H4SP2H = 3 AND H4SP2T =1 THEN HOUR24 = 0300;
IF H4SP2H = 4 AND H4SP2T =1 THEN HOUR24 = 0400;
IF H4SP2H = 5 AND H4SP2T =1 THEN HOUR24 = 0500;
IF H4SP2H = 6 AND H4SP2T =1 THEN HOUR24 = 0600;
IF H4SP2H = 7 AND H4SP2T =1 THEN HOUR24 = 0700;
IF H4SP2H = 8 AND H4SP2T =1 THEN HOUR24 = 0800;
IF H4SP2H = 9 AND H4SP2T =1 THEN HOUR24 = 0900;
IF H4SP2H = 10 AND H4SP2T =1 THEN HOUR24 = 1000;
IF H4SP2H = 11 AND H4SP2T =1 THEN HOUR24 = 1100;
IF H4SP2H = 12 AND H4SP2T =1 THEN HOUR24 = 0000;
IF H4SP2H = 1 AND H4SP2T =2 THEN HOUR24 = 1300;
IF H4SP2H = 2 AND H4SP2T =2 THEN HOUR24 = 1400;
IF H4SP2H = 3 AND H4SP2T =2 THEN HOUR24 = 1500;
IF H4SP2H = 4 AND H4SP2T =2 THEN HOUR24 = 1600;
IF H4SP2H = 5 AND H4SP2T =2 THEN HOUR24 = 1700;
IF H4SP2H = 6 AND H4SP2T =2 THEN HOUR24 = 1800;
IF H4SP2H = 7 AND H4SP2T =2 THEN HOUR24 = 1900;
IF H4SP2H = 8 AND H4SP2T =2 THEN HOUR24 = 2000;
IF H4SP2H = 9 AND H4SP2T =2 THEN HOUR24 = 2100;
IF H4SP2H = 10 AND H4SP2T =2 THEN HOUR24 = 2200;
IF H4SP2H = 11 AND H4SP2T =2 THEN HOUR24 = 2300;
IF H4SP2H = 12 AND H4SP2T =2 THEN HOUR24 = 1200;
WoScSleep=SUM(of H4SP2M HOUR24); /*no work or school next day*/
getuphs=WoScWake-WoScSleep;
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You can reduce all your IF statements to a couple of assignments statement using the HMS (hours/minutes/seconds) function to create sas times values, which are the number of seconds after midnight.  These values can be added/substract to get the number of seconds between time points, i.e. itervals in seconds.  Then you can format the resulting value to print out in military time form (basically HHMM without the colon).

 

proc format ;
  picture miltime
    low-high = '%0H%0M'  (datatype=time) ;
run;

data want;
  set have;
  time1=hms(mod(h4sp1h,12),h4sp1m,0);
  if h4sp1t=2 then time1=time1+'12:00:00't;

  time2=hms(mod(h4sp2h,12),h4sp2m,0);
  if h4sp2t=2 then time2=time2+'12:00:00't;

  getup_time=time1-time2;

  format time1 time2 getup_time miltime4.;
run;

 

This format works fine on TIME1 and TIME2.  And it works on GETUP_TIME if it is non-negative.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Either post a data step that creates your dataset, or post sample lines from the external file that contains your timestamps in a {i} window.

MS Office files are blocked against download at many corporate sites, for security reasons.

Lavdiff
Obsidian | Level 7
thank you will do
Kurt_Bremser
Super User

First of all, you should start by using SAS datetime values. As these are a count of seconds from a given starting point, they can easily be used in a subtraction, and the result just needs a time format to display in a human-readable form.

So you need to create timestamps from your military times; to assist you, we would need the original timestamps that contain your sleep/wake times.

Lavdiff
Obsidian | Level 7
can you provide me with an example? thank you
mkeintz
PROC Star

You can reduce all your IF statements to a couple of assignments statement using the HMS (hours/minutes/seconds) function to create sas times values, which are the number of seconds after midnight.  These values can be added/substract to get the number of seconds between time points, i.e. itervals in seconds.  Then you can format the resulting value to print out in military time form (basically HHMM without the colon).

 

proc format ;
  picture miltime
    low-high = '%0H%0M'  (datatype=time) ;
run;

data want;
  set have;
  time1=hms(mod(h4sp1h,12),h4sp1m,0);
  if h4sp1t=2 then time1=time1+'12:00:00't;

  time2=hms(mod(h4sp2h,12),h4sp2m,0);
  if h4sp2t=2 then time2=time2+'12:00:00't;

  getup_time=time1-time2;

  format time1 time2 getup_time miltime4.;
run;

 

This format works fine on TIME1 and TIME2.  And it works on GETUP_TIME if it is non-negative.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Lavdiff
Obsidian | Level 7
THANK YOU!!!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2098 views
  • 3 likes
  • 3 in conversation