## Working with military time

Solved
Occasional Contributor
Posts: 11

# Working with military time

[ Edited ]

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;`

Accepted Solutions
Solution
‎09-27-2017 11:21 AM
Posts: 1,309

## Re: Working with military time

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.

All Replies
Super User
Posts: 9,868

## Re: Working with military time

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 11

## Re: Working with military time

thank you will do
Super User
Posts: 9,868

## Re: Working with military time

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 11

## Re: Working with military time

can you provide me with an example? thank you
Solution
‎09-27-2017 11:21 AM
Posts: 1,309

## Re: Working with military time

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.

Occasional Contributor
Posts: 11

## Re: Working with military time

THANK YOU!!!
☑ This topic is solved.