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
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;
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.
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!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.