SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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