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

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

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