turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- To find the 1st working day of a month

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2016 11:49 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to lakshmiG

08-25-2016 12:36 AM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

08-25-2016 12:52 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to lakshmiG

08-25-2016 02:12 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Bruno_SAS

08-25-2016 02:48 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Bruno_SAS

08-25-2016 04:59 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to lakshmiG

08-25-2016 07:47 AM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

08-26-2016 03:49 AM

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

I will try this.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to lakshmiG

08-26-2016 04:33 AM

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;