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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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