- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
duration = (end - start) / 3600;
format duration time8.;
Apply a format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Assuming PROC CONTENTS shows this variable as numeric
difference=(date1-date2)/3600;
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
duration = (end - start) / 3600;
format duration time8.;
Apply a format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is wrong. Remove the division.
@Reeza wrote:
duration = (end - start) / 3600; format duration time8.;
Apply a format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;