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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
duration = (end - start) / 3600;
format duration time8.;

Apply a format.

View solution in original post

13 REPLIES 13
Kurt_Bremser
Super User

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
Fluorite | Level 6
Hello and thanks for the quick response. I had already tried INTCK. It only returns hours (rounded up) and not minutes.
ballardw
Super User

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

 

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
Kurt_Bremser
Super User

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

PaigeMiller
Diamond | Level 26

Assuming PROC CONTENTS shows this variable as numeric

 

difference=(date1-date2)/3600;
--
Paige Miller
JanSanford
Fluorite | Level 6
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.
Reeza
Super User
duration = (end - start) / 3600;
format duration time8.;

Apply a format.

Tom
Super User Tom
Super User

This is wrong. Remove the division.


@Reeza wrote:
duration = (end - start) / 3600;
format duration time8.;

Apply a format.


 

Tom
Super User Tom
Super User

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
Fluorite | Level 6
Perfect and elegantly simple. Thanks!
CurtisMackWSIPP
Lapis Lazuli | Level 10
Please mark the answer so other don't spend time seeing if you still need help.
ballardw
Super User

@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;

SAS Innovate 2025: Register Now

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!

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
  • 13 replies
  • 2268 views
  • 2 likes
  • 7 in conversation