How to do calculations with 24 hour HH:MM variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How to do calculations with 24 hour HH:MM variable

Hi

 

I have a dataset containing time variables in the 24 hour HH:MM. (e.g., 07:00, 18:30). I'm not sure how to do calculations with such variables. I try to get the difference between two such variables (like the date variable), but it didn't work out. Also, I'd like to add certain time to these variables (e.g., add 600 seconds to 07:00).

 

Maybe I can convert all these time variables to their SAS numeric values (seconds from midnight), but I don't know how.

 

Can anyone help with this?

 

Thanks a lot.

 

Terry


Accepted Solutions
Solution
‎06-28-2016 11:00 AM
Super User
Posts: 7,465

Re: How to do calculations with 24 hour HH:MM variable

data have;
input time_in $;
cards;
07:00
15:00
;
run;

data want;
set have;
format time_out hhmm5.;
time_out = input(time_in,time5.);
time_out = time_out + 700;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎06-28-2016 11:00 AM
Super User
Posts: 7,465

Re: How to do calculations with 24 hour HH:MM variable

data have;
input time_in $;
cards;
07:00
15:00
;
run;

data want;
set have;
format time_out hhmm5.;
time_out = input(time_in,time5.);
time_out = time_out + 700;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 6

Re: How to do calculations with 24 hour HH:MM variable

Thank you!

Super Contributor
Posts: 394

Re: How to do calculations with 24 hour HH:MM variable

Use the time5. informat to read the value and convert it to a SAS time value. 

 

data;
   input time time5.;
   put time=;
   put time hhmm5.;
   time2 = time + 600;
   put time2 hhmm5.;
datalines;
07:00
18:30
;;;;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 266 views
  • 0 likes
  • 3 in conversation