<?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: difference between two datetime fileds with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486169#M126443</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/225825"&gt;@invilink&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mathematically speaking, you want to integrate a step function (taking value 1 during work hours and 0 otherwise) over time intervals. So, you could compute the integrals of this function over the intervals [t&lt;FONT size="1 2 3 4 5 6 7"&gt;0&lt;/FONT&gt;, t] for a fixed point in time t&lt;FONT size="1 2 3 4 5 6 7"&gt;0&lt;/FONT&gt;&amp;nbsp;(e.g. the earliest datetime you need to consider) and all relevant datetimes t, of course using a reasonable time granularity, e.g. 1 min if you don't have non-zero seconds in your datetime values or 5 min if all datetime values are multiples of 5 min. Let f(t) denote that integral. Then you can compute the desired time differences between datetimes a and b as f(b)−f(a).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The table of values of f can be stored in a dataset (or just create a view) and looked up by means of a hash object, as shown below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let delta='0:05't; /* time granularity, here: 5 min; adapt as needed */

data holidays; /* extend this as needed or use your existing dataset */
input d yymmdd8.;
cards;
20180704
20181225
;

proc sql noprint;
select d into :holidays separated by ' '
from holidays;
quit;

data integral; /* or ... / view=integral; */
do t='01JAN18:00:00'dt to '01JAN19:00:00'dt by &amp;amp;delta; /* adapt start and end date as needed */
  d=datepart(t);
  f+(weekday(d) ~in (1,7) &amp;amp; d ~in (&amp;amp;holidays) &amp;amp; '10:00't&amp;lt;timepart(t)&amp;lt;='19:00't)*&amp;amp;delta;
  output;
end;
format t datetime20. f time.;
keep t f;
run;

data have;
input id (a b) (:datetime.);
format a b datetime20.;
cards;
1 10aug2018:15:00:00 10aug2018:18:00:00 
2 10aug2018:15:00:00 10aug2018:20:00:00 
3 11aug2018:15:00:00 11aug2018:18:00:00 
4 10aug2018:18:30:00 13aug2018:11:30:00 
5 10aug2018:15:00:00 14aug2018:14:00:00 
;

data want;
dcl hash h(dataset:'integral');
h.definekey('t');
h.definedata('f');
h.definedone();
call missing(t, f);
do until(last);
  set have end=last;
  diff=ifn(h.find(key: b)=0,f,.)-ifn(h.find(key: a)=0,f,.);
  output;
end;
stop;
format diff time.;
keep id a b diff;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 12 Aug 2018 16:41:47 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2018-08-12T16:41:47Z</dc:date>
    <item>
      <title>difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486043#M126371</link>
      <description>&lt;P&gt;Hi community!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to count the difference between two datetime values. But only the part, which are included in worktime &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;(from 10:00 to 19:00)&lt;/STRONG&gt;&lt;/FONT&gt; and workdays (a have a table with days marked as&amp;nbsp;workdays and holidays)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Examples&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;a1 =&amp;nbsp;'10aug2018:&lt;STRONG&gt;15:00:00&lt;/STRONG&gt;'dt&lt;/P&gt;&lt;P&gt;b1 =&amp;nbsp;'10aug2018:&lt;STRONG&gt;18:00:00&lt;/STRONG&gt;'dt&lt;/P&gt;&lt;P&gt;the&amp;nbsp;result must be 3 hours. Because 10aug2018 is workday and the range [15:00;17:00] is fully in range [10:00;19:00].&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;a2 =&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;'10aug2018:&lt;STRONG&gt;15:00:00&lt;/STRONG&gt;'dt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;b2 =&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;'10aug2018:&lt;STRONG&gt;20:00:00&lt;/STRONG&gt;'dt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;the&amp;nbsp;result must be 4 hours. Because 10aug2018 is workday and the range [15:00;20:00] is not fully in ragne [10:00;19:00]. In this case we count time from 15 to 19 – it is 4.&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;a3 =&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;'11aug2018:&lt;STRONG&gt;15:00:00&lt;/STRONG&gt;'dt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;b3 =&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;'11aug2018:&lt;STRONG&gt;18:00:00&lt;/STRONG&gt;'dt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;the rusult must be 0. Because 11aug2018 is Saturday, and is it not workday.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;a4 =&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;'10aug2018:&lt;STRONG&gt;18:30:00&lt;/STRONG&gt;'dt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;b4 =&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;'13aug2018:&lt;STRONG&gt;11:30:00&lt;/STRONG&gt;'dt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;the result must be 2 hours.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Because we have:&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;10aug2018: &lt;STRONG&gt;30 minutes&lt;/STRONG&gt; from 18:30 to 19:00&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;11aug2018 is&amp;nbsp;holiday: 0&lt;/LI&gt;&lt;LI&gt;12aug2018 is holiday: 0&lt;/LI&gt;&lt;LI&gt;13aug2018: &lt;STRONG&gt;1 hour 30 minutes&lt;/STRONG&gt; from 10:00 to 11:30&lt;/LI&gt;&lt;/UL&gt;&lt;/BLOCKQUOTE&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;a5 =&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;'&lt;STRONG&gt;10aug&lt;/STRONG&gt;2018:&lt;STRONG&gt;15:00:00&lt;/STRONG&gt;'dt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;b5 =&amp;nbsp;'&lt;STRONG&gt;14aug&lt;/STRONG&gt;2018:&lt;STRONG&gt;14:00:00&lt;/STRONG&gt;'dt&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;the result must be 17 hours&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;10aug2018:&amp;nbsp;&lt;STRONG&gt;4 hours&amp;nbsp;&lt;/STRONG&gt;from 15:00 to 19:00&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;11aug2018&amp;nbsp;is holiday&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;12aug2018 is holiday&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;13aug2018: &lt;STRONG&gt;9 hours&lt;/STRONG&gt; (from 10:00 to 19:00)&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;14aug2018: &lt;STRONG&gt;4&amp;nbsp;hours&lt;/STRONG&gt; (from 10:00 to 14:00)&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to solve this case with INTXN function, but I didn't succeed.&lt;/P&gt;&lt;P&gt;Hope for your help.&lt;/P&gt;</description>
      <pubDate>Sat, 11 Aug 2018 13:48:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486043#M126371</guid>
      <dc:creator>invilink</dc:creator>
      <dc:date>2018-08-11T13:48:25Z</dc:date>
    </item>
    <item>
      <title>Re: difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486050#M126375</link>
      <description>&lt;P&gt;Look at INTCK function&lt;/P&gt;</description>
      <pubDate>Sat, 11 Aug 2018 14:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486050#M126375</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-11T14:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486051#M126376</link>
      <description>&lt;P&gt;Tried both.&lt;/P&gt;&lt;P&gt;I tried everything I could before posting.&lt;/P&gt;</description>
      <pubDate>Sat, 11 Aug 2018 14:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486051#M126376</guid>
      <dc:creator>invilink</dc:creator>
      <dc:date>2018-08-11T14:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486052#M126377</link>
      <description>&lt;P&gt;One demo:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data w;&lt;BR /&gt;a1 = '10aug2018:15:00:00'dt;&lt;/P&gt;
&lt;P&gt;b1 = '10aug2018:18:00:00'dt;&lt;BR /&gt;hours=intck('hour',a1,b1);&lt;BR /&gt;put hours=;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Aug 2018 14:38:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486052#M126377</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-11T14:38:57Z</dc:date>
    </item>
    <item>
      <title>Re: difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486053#M126378</link>
      <description>&lt;P&gt;but how can I point that I need only worktime (from 10:00 to 19:00) and workdays?&lt;/P&gt;</description>
      <pubDate>Sat, 11 Aug 2018 14:41:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486053#M126378</guid>
      <dc:creator>invilink</dc:creator>
      <dc:date>2018-08-11T14:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486054#M126379</link>
      <description>&lt;P&gt;Can you post a sample of your dataset with 5 -6 records of what you have and and output sample of what you want explaining your requirement. You are most likely to get a better automated solution&lt;/P&gt;</description>
      <pubDate>Sat, 11 Aug 2018 14:50:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486054#M126379</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-11T14:50:07Z</dc:date>
    </item>
    <item>
      <title>Re: difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486092#M126394</link>
      <description>&lt;P&gt;How are your workdays defined?&lt;/P&gt;</description>
      <pubDate>Sat, 11 Aug 2018 21:40:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486092#M126394</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-08-11T21:40:05Z</dc:date>
    </item>
    <item>
      <title>Re: difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486099#M126399</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/225825"&gt;@invilink&lt;/a&gt;:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There're almost infinite number of ways you can specify the interval (the first argument) to the INTNX/CK functions. For 5-day work week starting on Monday, the interval is WEEKDAY5.2. To get work hours, you'll have to use the HOURx.y interval. Dip deeper into the docs to see how the period and subperiod X and Y should be specified for what you need. Here are good places to start digging:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/etsug/63939/HTML/default/viewer.htm#etsug_intervals_sect006.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/etsug/63939/HTML/default/viewer.htm#etsug_intervals_sect006.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/etsug/63939/HTML/default/viewer.htm#etsug_intervals_sect007.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/etsug/63939/HTML/default/viewer.htm#etsug_intervals_sect007.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH&lt;/P&gt;&lt;P&gt;Paul D.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Aug 2018 00:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486099#M126399</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-12T00:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: difference between two datetime fileds with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486169#M126443</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/225825"&gt;@invilink&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mathematically speaking, you want to integrate a step function (taking value 1 during work hours and 0 otherwise) over time intervals. So, you could compute the integrals of this function over the intervals [t&lt;FONT size="1 2 3 4 5 6 7"&gt;0&lt;/FONT&gt;, t] for a fixed point in time t&lt;FONT size="1 2 3 4 5 6 7"&gt;0&lt;/FONT&gt;&amp;nbsp;(e.g. the earliest datetime you need to consider) and all relevant datetimes t, of course using a reasonable time granularity, e.g. 1 min if you don't have non-zero seconds in your datetime values or 5 min if all datetime values are multiples of 5 min. Let f(t) denote that integral. Then you can compute the desired time differences between datetimes a and b as f(b)−f(a).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The table of values of f can be stored in a dataset (or just create a view) and looked up by means of a hash object, as shown below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let delta='0:05't; /* time granularity, here: 5 min; adapt as needed */

data holidays; /* extend this as needed or use your existing dataset */
input d yymmdd8.;
cards;
20180704
20181225
;

proc sql noprint;
select d into :holidays separated by ' '
from holidays;
quit;

data integral; /* or ... / view=integral; */
do t='01JAN18:00:00'dt to '01JAN19:00:00'dt by &amp;amp;delta; /* adapt start and end date as needed */
  d=datepart(t);
  f+(weekday(d) ~in (1,7) &amp;amp; d ~in (&amp;amp;holidays) &amp;amp; '10:00't&amp;lt;timepart(t)&amp;lt;='19:00't)*&amp;amp;delta;
  output;
end;
format t datetime20. f time.;
keep t f;
run;

data have;
input id (a b) (:datetime.);
format a b datetime20.;
cards;
1 10aug2018:15:00:00 10aug2018:18:00:00 
2 10aug2018:15:00:00 10aug2018:20:00:00 
3 11aug2018:15:00:00 11aug2018:18:00:00 
4 10aug2018:18:30:00 13aug2018:11:30:00 
5 10aug2018:15:00:00 14aug2018:14:00:00 
;

data want;
dcl hash h(dataset:'integral');
h.definekey('t');
h.definedata('f');
h.definedone();
call missing(t, f);
do until(last);
  set have end=last;
  diff=ifn(h.find(key: b)=0,f,.)-ifn(h.find(key: a)=0,f,.);
  output;
end;
stop;
format diff time.;
keep id a b diff;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Aug 2018 16:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/difference-between-two-datetime-fileds-with-condition/m-p/486169#M126443</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-08-12T16:41:47Z</dc:date>
    </item>
  </channel>
</rss>

