BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kajal_30
Quartz | Level 8

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
PeterClemmensen
Tourmaline | Level 20
%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.;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20
%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.;
Kurt_Bremser
Super User

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.

Ksharp
Super User
%let yearmonth = 202210;

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

%put &=last_month. ;
kajal_30
Quartz | Level 8

Thank you so much team.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 1663 views
  • 3 likes
  • 4 in conversation