Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Working with military time

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 09-26-2017 02:41 PM
(2503 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

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

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

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

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

thank you will do

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

can you provide me with an example? thank you

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

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

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

THANK YOU!!!

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.