BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hdg
Obsidian | Level 7 hdg
Obsidian | Level 7

I have a dataset called myds which has the below columns date code I would like to add another column called prevmonthdate which is the last business date of the previous month Thanks, HD

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Probably a better way than this, specifically to check if its a Saturday/Sunday. I also believe this question is answered on here, so searching will probably get you a better response.

data have;

    do i=1 to 12;

        date=mdy(i, 15, 2014);

        output;

    end;

run;

data want;

    set have;

    last_business_day=intnx('month', date, -1, 'end');

    if weekday(last_business_day) =1 then last_business_day=last_business_day-2;

    else if weekday(last_business_day)=7 then last_business_day=last_business_day-1;

    format date last_business_day date9.;

run;

View solution in original post

8 REPLIES 8
Reeza
Super User

Do you need to account for holidays?

hdg
Obsidian | Level 7 hdg
Obsidian | Level 7

no just business days..thanks

ballardw
Super User

So which are your business days?

hdg
Obsidian | Level 7 hdg
Obsidian | Level 7

sorry for being so unclear long day.. i mean just monday to friday only week days

Reeza
Super User

Probably a better way than this, specifically to check if its a Saturday/Sunday. I also believe this question is answered on here, so searching will probably get you a better response.

data have;

    do i=1 to 12;

        date=mdy(i, 15, 2014);

        output;

    end;

run;

data want;

    set have;

    last_business_day=intnx('month', date, -1, 'end');

    if weekday(last_business_day) =1 then last_business_day=last_business_day-2;

    else if weekday(last_business_day)=7 then last_business_day=last_business_day-1;

    format date last_business_day date9.;

run;

hdg
Obsidian | Level 7 hdg
Obsidian | Level 7

THank you. Both answers are correct!! Thanks very much

Reeza
Super User

SASKiwi answer is preferable to mine, more efficient.

SASKiwi
PROC Star

Alternative way:

Data want;

Date = "21Feb2014"d;

last_month_busday = intnx('WEEKDAY', intnx('MONTH', date, -1, 'END'), 0);

format last_month_busday date9.;

run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 8645 views
  • 4 likes
  • 4 in conversation