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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.