DATA Step, Macro, Functions and more

To find the 1st working day of a month

Reply
Contributor
Posts: 28

To find the 1st working day of a month

Dear Friends,

 

I am using the following code to find the beginning week day of every month, I am taking the latest daily data available in our database which is from  June 2016.

 

  %do i=0 %to 2;
    first_day_&i = intnx('weekday',intnx('month',day,-&i,'b'),0);
    format first_day_&i yymmdd10.;
    if date = first_day_&i then output;
  %end;

 

So, now I need values from APR2016 to JUN2016 (3 month values, code has to be automated to run to pick latest 3 months for current data) , so, 1st working day of June which is 01JUNE2016 and SAS could able to recognise and giving correct output, also for April 01APR2016 is weekday and its picking correctly. But for May2016, 1st is sunday and 2nd, Monday is a public holiday, so the date in database starts from 03May2016,  SAS instead to take 03MAY2016, it is considering 29APR2016, which is Friday of previous month, Why it is checking in previous month?

 

Please advise and guide me on this issue.

I need 03May2016 value as the output.

Super User
Posts: 9,671

Re: To find the 1st working day of a month

First of all , you need to know which date are Holidays , then Apply the following code:



data _null_;
x='01may2016'd;
first=intnx('weekday',x,0);
if weekday(first) in (1 6) then first=intnx('weekday',x,1);
/*
If first is holiday then do something here ........
*/
put x= date9. first= date9.;


x='01aug2016'd;
first=intnx('weekday',x,0);
if weekday(first) in (1 6) then first=intnx('weekday',x,1);
put x= date9. first= date9.;
run;

Contributor
Posts: 28

Re: To find the 1st working day of a month

Thanks for your guidance, I will try to apply and see if I could get the logic working correctly.Thanks once again!!!

SAS Super FREQ
Posts: 682

Re: To find the 1st working day of a month

SAS has the NWKDOM function that can give you back the first monday in a month. See the sample below.

 

Whether a date is a holiday that needs to come from somewhere else as it might be country and region dependant.

 

data want;
  year = year(today());
  do month = 1 to 12;
    firstDayMonth = mdy(month, 1, year);
    firstWorkDay = NWKDOM(1, 2, month, year);
    output;
  end;
  format first: weekdate.;
run;

Bruno

SAS Super FREQ
Posts: 682

Re: To find the 1st working day of a month

Here is a version for the first work day in the month

data want;
  do year = year(today())-1 to year(today());
    do month = 1 to 12;
      firstDayMonth = mdy(month, 1, year);
      firstMonday = NWKDOM(1, 2, month, year);

      if day(firstMonday) <= 3 then do;
        firstWorkDay = firstMonday;
      end;
      else do;
        firstWorkDay = intnx('weekday17w',firstDayMonth,0);
      end;

      output;
    end;
  end;

  format first: weekdate.;
run;

Bruno

Contributor
Posts: 28

Re: To find the 1st working day of a month

Hi Bruno,

 

Great!!! Thanks for your response and logic. Very interesting to know new logics in SAS.  I will use your logic in my coding and check. Thanks much!!!

 

Super User
Posts: 9,671

Re: To find the 1st working day of a month

Sorry. I made a mistake. The first code is not right . Try this one:



data _null_;
x='01aug2015'd;
first=intnx('weekday',x,0);
if month(first) ne month(x) then first=intnx('weekday',x,1);
/*
If first is holiday then do something here ........
*/
put x= date9. first= date9.;


x='01jul2016'd;
first=intnx('weekday',x,0);
if month(first) ne month(x) then first=intnx('weekday',x,1);
put x= date9. first= date9.;
run;




Contributor
Posts: 28

Re: To find the 1st working day of a month

Great!!! Thanks much Xia Keshan for your wonderful support!!!

I will try this.

Super User
Posts: 9,671

Re: To find the 1st working day of a month

If you only care about the first day of each month, That might be easy.


data _null_;
x='01aug2015'd;
first=intnx('weekday',x,0);
if month(first) ne month(x) then first=intnx('weekday',x,1);
if weekday(x) in (1 7) then first=first+1;
put x= date9. first= date9.;


x='01may2016'd;
first=intnx('weekday',x,0);
if month(first) ne month(x) then first=intnx('weekday',x,1);
if weekday(x) in (1 7) then first=first+1;
put x= date9. first= date9.;
run;


Ask a Question
Discussion stats
  • 8 replies
  • 663 views
  • 6 likes
  • 3 in conversation