BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HitmonTran
Pyrite | Level 9

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_timeend_date_time
13JUN2019:15:41:0021JUN2019:14:37:00
15MAR2022:02:28:0016MAR2022:16:24:00

 

want

start_date_timeend_date_timehours
13JUN2019:15:41:0021JUN2019:14:37:00 
15MAR2022:02:28:0016MAR2022:16:24:00 
1 ACCEPTED SOLUTION
2 REPLIES 2
yabwon
Onyx | Level 15

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

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 2 replies
  • 485 views
  • 1 like
  • 3 in conversation