<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how to calculate # of hours withe datetime22. variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-of-hours-withe-datetime22-variable/m-p/925958#M364362</link>
    <description>&lt;P&gt;If your datetime variables are just a character text, here are two options:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If your variables are genuine (numeric) datetime variables here are three options:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 26 Apr 2024 07:15:54 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2024-04-26T07:15:54Z</dc:date>
    <item>
      <title>how to calculate # of hours withe datetime22. variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-of-hours-withe-datetime22-variable/m-p/925951#M364356</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to calculate duration of hours from start datetime to end datetime. The format of the variables are numeric datetime22.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data:&lt;/STRONG&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;start_date_time&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;end_date_time&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13JUN2019:15:41:00&lt;/TD&gt;&lt;TD&gt;21JUN2019:14:37:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15MAR2022:02:28:00&lt;/TD&gt;&lt;TD&gt;&lt;SPAN&gt;16MAR2022:16:24:00&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;want&lt;/STRONG&gt;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;start_date_time&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;end_date_time&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;hours&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;13JUN2019:15:41:00&lt;/TD&gt;&lt;TD&gt;21JUN2019:14:37:00&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;15MAR2022:02:28:00&lt;/TD&gt;&lt;TD&gt;&lt;SPAN&gt;16MAR2022:16:24:00&lt;/SPAN&gt;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 26 Apr 2024 06:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-of-hours-withe-datetime22-variable/m-p/925951#M364356</guid>
      <dc:creator>HitmonTran</dc:creator>
      <dc:date>2024-04-26T06:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to calculate # of hours withe datetime22. variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-of-hours-withe-datetime22-variable/m-p/925958#M364362</link>
      <description>&lt;P&gt;If your datetime variables are just a character text, here are two options:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If your variables are genuine (numeric) datetime variables here are three options:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2024 07:15:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-of-hours-withe-datetime22-variable/m-p/925958#M364362</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-04-26T07:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to calculate # of hours withe datetime22. variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-of-hours-withe-datetime22-variable/m-p/925960#M364364</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;hours = intck("hour",start_date_time,end_date_time,"c");&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Apr 2024 07:34:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-calculate-of-hours-withe-datetime22-variable/m-p/925960#M364364</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-04-26T07:34:53Z</dc:date>
    </item>
  </channel>
</rss>

