<?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 INTNX &amp;amp; INTCK using Custom Workday Intervals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/INTNX-amp-INTCK-using-Custom-Workday-Intervals/m-p/488305#M127263</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been asked to measure workdays between event timestamps according to a variety of business rules. To accomplish this I am trying to create custom intervals that will allow me to measure from the start of the&amp;nbsp;next workday to the end of the&amp;nbsp;last workday and then subtract out non-workdays between them using INTNX &amp;amp; INTCK functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** concept ***/

first_workday_dt = intnx('workday',start_timestamp,+1,'beginning');
final_workday_dt = intnx('workday',stop_timestamp,-1,'end');

workday_minutes = intck('dtminutes',first_workday_dt,final_workday_dt) - (intck('non_workdays',first_workday_dt,final_workday_dt)*1440);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset containing all dates for the current and previous three years with a flag indicating weekends and holidays. From this I've created two new datasets with begin and end times for workdays and non-workdays. Creating intervals from these allows me to perform the calculations described above, however, I am seeing null results when the input date is not in the interval dataset or incorrect results when the result would extend beyond the interval dataset. While this makes sense intuitively, I am at a loss as to how to fix or work around it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is what I am trying to do possible, and if so, how? I am also open to other approaches as well, we are just trying to find the most efficient and repeatable way to do this as it is a common ask. Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** flag weekends and holidays ***/
data work.Calendar (keep=date flag); merge work.Days work.Holidays (in=holiday); by date;

  format Date date. Flag $10.;

  if weekday(date) in (1,7) then flag = 'Weekend';
    else if holiday = 1 then flag = type;
      else flag = '';

run;


/*** create sas interval datasets ***/
proc sql;
create table work.Calendar_Workdays as select dhms(date,00,00,00) as begin format = datetime., dhms(date,23,59,59) as end format = datetime. from work.Calendar where flag is missing;
create table work.Calendar_NonWorkdays as select dhms(date,00,00,00) as begin format = datetime., dhms(date,23,59,59) as end format = datetime. from work.Calendar where flag is not missing;
quit;

/*** create sas intervals ***/
options intervalds = (workday=work.calendar_workdays non_workday=work.calendar_nonworkdays);

/*** advance to next workday and back to previous workday ***/
data work.test; set work.days;
format next_business_day last_business_day datetime.;
  next_business_day = intnx('workday',dhms(date,12,00,00),+1,'beginning');
  last_business_day = intnx('workday',dhms(date,12,00,00),-1,'end');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Results:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 328px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22640iBF0B04EF8377CA05/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 20 Aug 2018 18:50:18 GMT</pubDate>
    <dc:creator>VictorSirianni</dc:creator>
    <dc:date>2018-08-20T18:50:18Z</dc:date>
    <item>
      <title>INTNX &amp; INTCK using Custom Workday Intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/INTNX-amp-INTCK-using-Custom-Workday-Intervals/m-p/488305#M127263</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been asked to measure workdays between event timestamps according to a variety of business rules. To accomplish this I am trying to create custom intervals that will allow me to measure from the start of the&amp;nbsp;next workday to the end of the&amp;nbsp;last workday and then subtract out non-workdays between them using INTNX &amp;amp; INTCK functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** concept ***/

first_workday_dt = intnx('workday',start_timestamp,+1,'beginning');
final_workday_dt = intnx('workday',stop_timestamp,-1,'end');

workday_minutes = intck('dtminutes',first_workday_dt,final_workday_dt) - (intck('non_workdays',first_workday_dt,final_workday_dt)*1440);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset containing all dates for the current and previous three years with a flag indicating weekends and holidays. From this I've created two new datasets with begin and end times for workdays and non-workdays. Creating intervals from these allows me to perform the calculations described above, however, I am seeing null results when the input date is not in the interval dataset or incorrect results when the result would extend beyond the interval dataset. While this makes sense intuitively, I am at a loss as to how to fix or work around it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is what I am trying to do possible, and if so, how? I am also open to other approaches as well, we are just trying to find the most efficient and repeatable way to do this as it is a common ask. Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** flag weekends and holidays ***/
data work.Calendar (keep=date flag); merge work.Days work.Holidays (in=holiday); by date;

  format Date date. Flag $10.;

  if weekday(date) in (1,7) then flag = 'Weekend';
    else if holiday = 1 then flag = type;
      else flag = '';

run;


/*** create sas interval datasets ***/
proc sql;
create table work.Calendar_Workdays as select dhms(date,00,00,00) as begin format = datetime., dhms(date,23,59,59) as end format = datetime. from work.Calendar where flag is missing;
create table work.Calendar_NonWorkdays as select dhms(date,00,00,00) as begin format = datetime., dhms(date,23,59,59) as end format = datetime. from work.Calendar where flag is not missing;
quit;

/*** create sas intervals ***/
options intervalds = (workday=work.calendar_workdays non_workday=work.calendar_nonworkdays);

/*** advance to next workday and back to previous workday ***/
data work.test; set work.days;
format next_business_day last_business_day datetime.;
  next_business_day = intnx('workday',dhms(date,12,00,00),+1,'beginning');
  last_business_day = intnx('workday',dhms(date,12,00,00),-1,'end');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Results:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 328px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22640iBF0B04EF8377CA05/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Aug 2018 18:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/INTNX-amp-INTCK-using-Custom-Workday-Intervals/m-p/488305#M127263</guid>
      <dc:creator>VictorSirianni</dc:creator>
      <dc:date>2018-08-20T18:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: INTNX &amp; INTCK using Custom Workday Intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/INTNX-amp-INTCK-using-Custom-Workday-Intervals/m-p/488368#M127284</link>
      <description>&lt;P&gt;Your calendar data sets have gaps, i.e. instances in which END for one observation is more than 1 unit (1 second) less than BEGIN for the next observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could create END as&amp;nbsp;next BEGIN-1 (much easier with a data step than in sql).&amp;nbsp; But you can also just drop the END variable.&amp;nbsp; Then sas will assume that end is just 1 less than the next begin, yielding no gaps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Aug 2018 21:27:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/INTNX-amp-INTCK-using-Custom-Workday-Intervals/m-p/488368#M127284</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-08-20T21:27:36Z</dc:date>
    </item>
  </channel>
</rss>

