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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 439 views
  • 0 likes
  • 4 in conversation