BookmarkSubscribeRSS Feed
ScotchCat
Obsidian | Level 7

I’m attempting to calculate the difference between two datetimes by day, hour, minute and second.  The datetimes are in DATETIME26.6 (ddMMMyyyy:00:00:00.000000) format.

I’d like to result to be day.hour,min,sec   

Example:

StartDate                                             EndDate                                               Wanted Result – D.hms

3/24/2021 13:24:49.287000          3/24/2021 13:30:46.886000                       0.004                    

2/4/2021 15:25:58.501000            3/8/2021 13:59:51.848000                         31.941

 

I’ve tried Intck, however seems to be limited to only one increment and looking at the whole number instead of breaking it down by the time increments I need.   Any suggestions are welcome! thanks

3 REPLIES 3
ballardw
Super User

One value - the other gives you a number of seconds (and any fraction). So simple arithmetic knowing the number of seconds in a day.

 

data junk;
   x='24MAR2021:13:24:49.287000'dt;
   y='24MAR2021:13:30:46.886000'dt;
   diff = (y-x)/(24*60*60);
run;
Reeza
Super User
Does 31.941 mean 31 days, 9 hours, 4 minutes and 1 second? How would you display 31 days, 9 hours and 41 minutes even?

day.hour,min,sec is a very unique format so I'm assuming I'm wrong here....

Kurt_Bremser
Super User
data want;
input start_date : mmddyy10. start_time :time15. end_date :mmddyy10. end_time :time15.;
start_dt = dhms(start_date,0,0,start_time);
end_dt = dhms(end_date,0,0,end_time);
diff = (end_dt - start_dt) / 86400;
format
  start_date end_date yymmdd10.
  start_time end_time time15.6
  start_dt end_dt e8601dt26.6
  diff 10.3
;
datalines;
3/24/2021 13:24:49.287000          3/24/2021 13:30:46.886000
2/4/2021 15:25:58.501000            3/8/2021 13:59:51.848000
;

Please do us a favor and post your data as data step with datalines, so we can recreate it by copy/pasting and submitting the code.

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
  • 3 replies
  • 1126 views
  • 0 likes
  • 4 in conversation