Help using Base SAS procedures

get last available business date in previous month

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 79
Accepted Solution

get last available business date in previous month

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


Accepted Solutions
Solution
‎02-20-2014 05:28 PM
Super User
Posts: 17,784

Re: get last available business date in previous month

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


All Replies
Super User
Posts: 17,784

Re: get last available business date in previous month

Do you need to account for holidays?

Frequent Contributor
Frequent Contributor
Posts: 79

Re: get last available business date in previous month

no just business days..thanks

Super User
Posts: 10,483

Re: get last available business date in previous month

So which are your business days?

Frequent Contributor
Frequent Contributor
Posts: 79

Re: get last available business date in previous month

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

Solution
‎02-20-2014 05:28 PM
Super User
Posts: 17,784

Re: get last available business date in previous month

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;

Frequent Contributor
Frequent Contributor
Posts: 79

Re: get last available business date in previous month

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

Super User
Posts: 17,784

Re: get last available business date in previous month

SASKiwi answer is preferable to mine, more efficient.

Super User
Posts: 3,102

Re: get last available business date in previous month

Alternative way:

Data want;

Date = "21Feb2014"d;

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

format last_month_busday date9.;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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