Hello,
I am trying to calculate duration of hours from start datetime to end datetime. The format of the variables are numeric datetime22.
data:
| start_date_time | end_date_time |
| 13JUN2019:15:41:00 | 21JUN2019:14:37:00 |
| 15MAR2022:02:28:00 | 16MAR2022:16:24:00 |
want
| start_date_time | end_date_time | hours |
| 13JUN2019:15:41:00 | 21JUN2019:14:37:00 | |
| 15MAR2022:02:28:00 | 16MAR2022:16:24:00 |
hours = intck("hour",start_date_time,end_date_time,"c");
If your datetime variables are just a character text, here are two options:
data have1;
input (start_date_time end_date_time) (:$22.);
cards;
13JUN2019:15:41:00 21JUN2019:14:37:00
15MAR2022:02:28:00 16MAR2022:16:24:00
;
run;
proc print;
run;
data want1;
set have1;
/* optioin 1 */
hours1 = (input(end_date_time,datetime22.)
-
input(start_date_time,datetime22.)
) / 3600;
/* optioin 2 */
tmp = put((input(end_date_time,datetime22.)
-
input(start_date_time,datetime22.)
), time12.);
hours2=input(scan(tmp,1,":"),best12.);
minutes2=input(scan(tmp,2,":"),best12.);
seconds2=input(scan(tmp,3,":"),best12.);
run;
proc print;
run;
If your variables are genuine (numeric) datetime variables here are three options:
data have;
input (start_date_time end_date_time) (:datetime22.);
format start_date_time end_date_time datetime22.;
cards;
13JUN2019:15:41:00 21JUN2019:14:37:00
15MAR2022:02:28:00 16MAR2022:16:24:00
;
run;
proc print;
run;
data want;
set have;
/* optioin 1 */
hours1 = (end_date_time - start_date_time) / 3600;
/* optioin 2 */
tmp = put(end_date_time - start_date_time, time12.);
hours2=input(scan(tmp,1,":"),best12.);
minutes2=input(scan(tmp,2,":"),best12.);
seconds2=input(scan(tmp,3,":"),best12.);
/* optioin 3 */
tmp2 = end_date_time - start_date_time;
hours3=int(tmp2/3600);
minutes3=int((tmp2-3600*hours3)/60);
seconds3=tmp2 - 3600*hours3 - 60*minutes3;
run;
proc print;
run;
Bart
hours = intck("hour",start_date_time,end_date_time,"c");
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 save with the early bird rate—just $795!
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.