BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Vergriffenego
Fluorite | Level 6

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).

idso (hh:mm)half (hrs)new (hh:mm)
71:303.32 
261:003.73 
310:003.94 
683:003.32 
870:503.23 

 

How can I calculate the "new" variable?

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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).

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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).

Vergriffenego
Fluorite | Level 6

 This worked. Thank you very much!

s_lassen
Meteorite | Level 14

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;   
Vergriffenego
Fluorite | Level 6

Thank you for your reply! The solution worked well!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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