Fluorite | Level 6

## How to add hours to a time value to get a new time

Hello,

I am working on a dataset that need to calculate a new time. For example, I slept at 00:00 (so) and slept for 6.5 (half) hours, so I got up at 06:00 (new).

 id so (hh:mm) half (hrs) new (hh:mm) 7 1:30 3.32 26 1:00 3.73 31 0:00 3.94 68 3:00 3.32 87 0:50 3.23

How can I calculate the "new" variable?

I tried simply to add " so+half" then I got seconds.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: How to add hours to a time value to get a new time

Time values in SAS are counts of seconds, so you need to expand your hours to this:

``````new = so + (half * 3600);
format new time5.;``````

The brackets are just there for clarification, mathematically they're not needed (multiplication is done before addition).

4 REPLIES 4
Super User

## Re: How to add hours to a time value to get a new time

Time values in SAS are counts of seconds, so you need to expand your hours to this:

``````new = so + (half * 3600);
format new time5.;``````

The brackets are just there for clarification, mathematically they're not needed (multiplication is done before addition).

Fluorite | Level 6

## Re: How to add hours to a time value to get a new time

This worked. Thank you very much!

Meteorite | Level 14

## Re: How to add hours to a time value to get a new time

Assuming your input data looks like this (it is better to present is as a data step):

``````data have;
input id so : time. half;
format so time5.;
cards;
7      1:30      3.32
26      1:00      3.73
31      0:00      3.94
68      3:00      3.32
87      0:50      3.23
;run;
``````

You can simply convert the hours to seconds (60*60=3600 seconds/hour) and add to get the new time:

``````data want;
set have;
new=so+half*3600;
format new time5.;
run;
``````
Fluorite | Level 6

## Re: How to add hours to a time value to get a new time

Discussion stats
• 4 replies
• 544 views
• 2 likes
• 3 in conversation