<?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: calculate number of business days between 2 dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823161#M325035</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;There are 2 data sets:&lt;/P&gt;
&lt;P&gt;date set "Dates" include dates that are business days in specific country.&lt;/P&gt;
&lt;P&gt;Date set&amp;nbsp; have includes 3 fields: customer_ID&amp;nbsp; , date1, date2.&lt;/P&gt;
&lt;P&gt;The target is to calculate how many business days&amp;nbsp; between date1 and date2.&lt;/P&gt;
&lt;P&gt;It means that function &lt;FONT size="5" color="#FF0000"&gt;&lt;STRONG&gt;INTNX&lt;/STRONG&gt; &lt;/FONT&gt;will not help becuase it can calculate number of days between date1 and date2&amp;nbsp; but I need to calculate number of business days between day2 and day2.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Intnx never calculates "number of days" between anything. It increments values by a number of intervals. You mean to reference the INTCK function. If you have been using INTNX I would expect some very strange results indeed.&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jul 2022 16:05:18 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-07-13T16:05:18Z</dc:date>
    <item>
      <title>calculate number of business days between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823051#M324994</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;There are 2 data sets:&lt;/P&gt;
&lt;P&gt;date set "Dates" include dates that are business days in specific country.&lt;/P&gt;
&lt;P&gt;Date set&amp;nbsp; have includes 3 fields: customer_ID&amp;nbsp; , date1, date2.&lt;/P&gt;
&lt;P&gt;The target is to calculate how many business days&amp;nbsp; between date1 and date2.&lt;/P&gt;
&lt;P&gt;It means that function INTNX will not help becuase it can calculate number of days between date1 and date2&amp;nbsp; but I need to calculate number of business days between day2 and day2.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;For ID=1&amp;nbsp; the difference should be 4&amp;nbsp; between 02OCT and 24SEP&lt;/P&gt;
&lt;P&gt;'24SEP2020'd&lt;BR /&gt;'25SEP2020'd&lt;BR /&gt;'29SEP2020'd&lt;BR /&gt;'30SEP2020'd&lt;BR /&gt;'02OCT2020'd&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For ID=2 the difference should be 1 between&amp;nbsp; &amp;nbsp;09OCT and 08OCT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what is the way to calculate it please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data dates;
format date date9.;
input date : date9.;
cards;
'01SEP2020'd
'02SEP2020'd
'03SEP2020'd
'04SEP2020'd
'06SEP2020'd
'07SEP2020'd
'08SEP2020'd
'09SEP2020'd
'10SEP2020'd
'11SEP2020'd
'13SEP2020'd
'14SEP2020'd
'15SEP2020'd
'16SEP2020'd
'17SEP2020'd
'18SEP2020'd
'21SEP2020'd
'22SEP2020'd
'23SEP2020'd
'24SEP2020'd
'25SEP2020'd
'29SEP2020'd
'30SEP2020'd
'02OCT2020'd
'04OCT2020'd
'05OCT2020'd
'06OCT2020'd
'07OCT2020'd
'08OCT2020'd
'09OCT2020'd
'11OCT2020'd
;
Run;


data have;
format date1 date2 date9.;
Input ID date1  : date9. date2  : date9.;
cards;
1 '02OCT2020'd '24SEP2020'd
2 '09OCT2020'd '08OCT2020'd
3 '13SEP2020'd '10SEP2020'd
;
Run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jul 2022 06:02:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823051#M324994</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-07-13T06:02:22Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of business days between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823052#M324995</link>
      <description>&lt;P&gt;Just an idea:&lt;/P&gt;
&lt;P&gt;- use "dates" as hash object&lt;/P&gt;
&lt;P&gt;- loop from date2 to date1&lt;/P&gt;
&lt;P&gt;- increment a counter if hash.check() return 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 06:13:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823052#M324995</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-07-13T06:13:10Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of business days between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823055#M324997</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;For ID=1 the difference should be 4 between 02OCT and 24SEP&lt;/P&gt;
&lt;P&gt;'24SEP2020'd&lt;BR /&gt;'25SEP2020'd&lt;BR /&gt;'29SEP2020'd&lt;BR /&gt;'30SEP2020'd&lt;BR /&gt;'02OCT2020'd&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why 4?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 06:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823055#M324997</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-07-13T06:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of business days between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823058#M324998</link>
      <description>&lt;P&gt;If you only want to exclude Saturdays and Sundays, look at the WEEKDAY interval for INTCK.&lt;/P&gt;
&lt;P&gt;If you also need to exclude local holidays, set up a table of business days, load it into a hash, and loop over the whole time range and count the found business days.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 06:56:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823058#M324998</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-07-13T06:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of business days between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823066#M325002</link>
      <description>&lt;P&gt;One way is to create a date "dimension", containing all the relevant dates (here shown with the dates in 2020):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data all_dates(index=(date));
  retain start_date '31dec2019'd;
  set dates end=done;&lt;BR /&gt;  where date between '01jan2020'd and '31dec2020'd; /* we are only creating for 2020 here */
  business_day=0;
  do date=start_date+1 to date-1;
    output;
    end;
  business_day=1;
  output;
  start_date=date;
  if done;
  business_day=0;
  do date=date+1 to '31dec2020'd;
    output;
    end;
  drop start_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then use that to get the number of business days:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  n_business_days=0;
  do date=date2 to date1;
    set all_dates key=date/unique;
    n_business_days+business_day;
    end;
  drop date business_day;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do not want to count DATE1, even if it is a business day, just change the code to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  n_business_days=0;
  do date=date2 to date1-1;
    set all_dates key=date/unique;
    n_business_days+business_day;
    end;
  drop date business_day;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(which will give the result 4 for ID=1)&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 08:27:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823066#M325002</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-07-13T08:27:14Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of business days between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823079#M325009</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;: I like the idea of an indexed lookup dataset because, once created, it can be used many times. Maybe for &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;'s application it would be even more useful if it contained &lt;EM&gt;cumulative&lt;/EM&gt; numbers of business days. The calculation of the number of business days between two dates would then reduce to a mere subtraction of &lt;EM&gt;two&lt;/EM&gt; values retrieved from ALL_DATES instead of the "&lt;FONT face="courier new,courier"&gt;do date=date2 to date1;&lt;/FONT&gt;" loop with possibly &lt;EM&gt;many&lt;/EM&gt; iterations and data retrievals. This would require only a few modifications of your code (highlighted below)&lt;FONT face="helvetica"&gt;:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#999999"&gt;data all_dates(index=(date));
  retain start_date '31dec2019'd;
  set dates end=done;  where date between '01jan2020'd and '31dec2020'd; /* we are only creating for 2020 here */
  &lt;FONT color="#000000"&gt;/* business_day=0; */&lt;/FONT&gt;
  do date=start_date+1 to date-1;
    output;
    end;
  business_day&lt;FONT size="4"&gt;&lt;STRONG&gt;&lt;FONT color="#000000"&gt;+&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;1;
  output;
  start_date=date;
  if done;
  &lt;FONT color="#000000"&gt;/* business_day=0; */&lt;/FONT&gt;
  do date=date+1 to '31dec2020'd;
    output;
    end;
  drop start_date;
run;

data want;
  set have;
  &lt;FONT color="#000000"&gt;date=date1;&lt;/FONT&gt;
  set all_dates key=date/unique;
  &lt;FONT color="#000000"&gt;_n_=business_day;
  date=date2;
  set all_dates key=date/unique;
  &lt;FONT color="#999999"&gt;n_business_days&lt;FONT color="#000000"&gt;=&lt;/FONT&gt;&lt;/FONT&gt;_n_-business_day;&lt;/FONT&gt;
  drop date business_day;
run;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;The last data step yields the counts excluding DATE2, which meets the specifications.*&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* Edit: Actually, the two examples given in the initial post would also be consistent with an exclusion of DATE1, but the code above could be adapted easily to that, e.g., by subtracting 1 from DATE1 and DATE2, resp., in the assignment statements for DATE in the second data step.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 11:05:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823079#M325009</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-13T11:05:26Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of business days between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823161#M325035</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;There are 2 data sets:&lt;/P&gt;
&lt;P&gt;date set "Dates" include dates that are business days in specific country.&lt;/P&gt;
&lt;P&gt;Date set&amp;nbsp; have includes 3 fields: customer_ID&amp;nbsp; , date1, date2.&lt;/P&gt;
&lt;P&gt;The target is to calculate how many business days&amp;nbsp; between date1 and date2.&lt;/P&gt;
&lt;P&gt;It means that function &lt;FONT size="5" color="#FF0000"&gt;&lt;STRONG&gt;INTNX&lt;/STRONG&gt; &lt;/FONT&gt;will not help becuase it can calculate number of days between date1 and date2&amp;nbsp; but I need to calculate number of business days between day2 and day2.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Intnx never calculates "number of days" between anything. It increments values by a number of intervals. You mean to reference the INTCK function. If you have been using INTNX I would expect some very strange results indeed.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 16:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823161#M325035</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-07-13T16:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of business days between 2 dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823273#M325071</link>
      <description>&lt;P&gt;I actually agree with you. But I think the right way is to go all the way "dimensional", and have both the number of business days (since some arbitrary start date) and the flag that indicates whether a specific date is a business day (and possibly the same markers for different countries), as a typical date dimension will never have more than a few thousand rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the easy way to get the result may be to create a view, something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create view want as select have.*,
     (select n_business_day from dates where date=have.date1)-
     (select n_business_day from dates where date=have.date2) as n_business_days;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course you would then save both the underlying tables and the view on a permanent library or a server.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason I did it with the flag solution was just that it made for the simplest code initially.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 11:00:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-business-days-between-2-dates/m-p/823273#M325071</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-07-14T11:00:45Z</dc:date>
    </item>
  </channel>
</rss>

