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.
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).
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).
This worked. Thank you very much!
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;
Thank you for your reply! The solution worked well!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.