Quartz | Level 8

## getting last month from year month date

Hi All,

I have  a date in the form of year month and I want the last month date from the code . for eg I have 202210 and I want the code to provide me 202209.

tried code below but didn't work.

%let yearmonth = 202210;

data one;
call symputx('P_Month', month(intnx('month',&yearmonth,-1)));
run;

%put &P_Month;

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: getting last month from year month date

%let yearmonth = 202210;

data _null_;
dt = input(put(&yearmonth., 6.), yymmn6.);
call symputx('P_Month', put(intnx('month', dt, -1, 'b'), yymmn6.));
run;

%put &P_Month.;
4 REPLIES 4
Tourmaline | Level 20

## Re: getting last month from year month date

%let yearmonth = 202210;

data _null_;
dt = input(put(&yearmonth., 6.), yymmn6.);
call symputx('P_Month', put(intnx('month', dt, -1, 'b'), yymmn6.));
run;

%put &P_Month.;
Super User

## Re: getting last month from year month date

SAS dates are counts of days, with 1960-01-01 being day zero.

So your INTNX function starts with a date that is 202,210 days after 1960-01-01, so quite far in the future.

As already indicated, you must convert your year/month string to a SAS date first by using the INPUT function with a proper informat.

Super User

## Re: getting last month from year month date

%let yearmonth = 202210;

%let last_month=%sysfunc(intnx(month, %sysfunc(inputn(&yearmonth.,yymmn6.)) ,-1),yymmn6.) ;

%put &=last_month. ;
Quartz | Level 8

## Re: getting last month from year month date

Thank you so much team.

Discussion stats
• 4 replies
• 714 views
• 3 likes
• 4 in conversation