BookmarkSubscribeRSS Feed
lakshmiG
Obsidian | Level 7

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.

8 REPLIES 8
Ksharp
Super User
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;

lakshmiG
Obsidian | Level 7

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

BrunoMueller
SAS Super FREQ

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

BrunoMueller
SAS Super FREQ

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

lakshmiG
Obsidian | Level 7

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!!!

 

Ksharp
Super User
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;




lakshmiG
Obsidian | Level 7

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

I will try this.

Ksharp
Super User
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;


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 6421 views
  • 6 likes
  • 3 in conversation