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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 7572 views
  • 4 likes
  • 4 in conversation