- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This worked. Thank you very much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for your reply! The solution worked well!