I have two dates in the following format:
Date 1 - 27JAN2019:22:09:00.00
Date 2 - 28JAN2019:00:24:00.00
How can i calculate difference between these two date in HOURS?
Thanks in Advance for any solution provided.
duration = (end - start) / 3600;
format duration time8.;
Apply a format.
Use the INTCK function:
data test;
dt1 = '27JAN2019:22:09:00.00'dt;
dt2 = '28JAN2019:00:24:00.00'dt;
diff = intck('hour',dt1,dt2);
format dt1 dt2 datetime23.2;
run;
Or simply calculate (dt2 - dt1) / 3600, as times and datetimes in SAS are counts of seconds.
@JanSanford wrote:
Hello and thanks for the quick response. I had already tried INTCK. It only returns hours (rounded up) and not minutes.
Since your request explicitly states hours then what is the problem with returning hours?:
How can i calculate difference between these two date in HOURS?
perhaps you need to SHOW exactly what you expect the value to be.
@JanSanford wrote:
Hello and thanks for the quick response. I had already tried INTCK. It only returns hours (rounded up) and not minutes.
This was not a stated requirement of the original problem. You will get better and faster answers when you specify the entire set of requirements in your original question.
@JanSanford wrote:
Hello and thanks for the quick response. I had already tried INTCK. It only returns hours (rounded up) and not minutes.
Quote from your initial post:
How can i calculate difference between these two date in HOURS?
If you just want the time difference in hh:mm format, subtract the two dates and assign the TIME5. format to the result.
Assuming PROC CONTENTS shows this variable as numeric
difference=(date1-date2)/3600;
duration = (end - start) / 3600;
format duration time8.;
Apply a format.
This is wrong. Remove the division.
@Reeza wrote:
duration = (end - start) / 3600; format duration time8.;
Apply a format.
DATETIME values are seconds. So just take the difference and apply the TIME format to have the number of seconds print in the tradition HH:MM:SS style.
difference=datetime1-datetime2;
format difference time8.;
If the difference might be more than 99 hours then use a wider format, TIME12. for example.
@JanSanford wrote:
Hello and thanks for the quick response. I had already tried dividing by seconds. It returns a value with fractions, not minutes. IE 1.3333 rather than 01:20.
Lets resort to a grade-school instruction:
SHOW your work.
We have no idea what you did.
Of course you get decimals, you're doing division.
And are you now saying you want "minutes" as the result? The INTCK function will return intervals of seconds, minutes and a bunch of other intervals.
data example; d1 ='01JAN2021:12:15:25'dt; d2 ='02JAN2021:10:09:18'dt; m = intck('minutes',d1,d2); s = intck('seconds',d1,d2); h = intck('hours',d1,d2); run;
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.