BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
somepathak
Calcite | Level 5

Hi All, 

 

I am new to SAS and need help with subtracting months from a macro variable storing date in YYYYMM format. I tried the following code but it isn't working, any help on this would be appreciated. 

 

The desired value for last_mon is 201909

%let score_month = 201910; /*YYYYMM format */
%let mon_var = %sysfunc(inputn(putn(&score_month,6.)!!'01',yymmdd8.)); /*converting to sas date format by adding day as 01 */
%let last_mon = %sysfunc(intnx(month,&mon_var,-1),yymmn6.); /*subtracting 1 month */ 
%put &score_month***&last_mon***
Notes :
 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 %let score_month = 201910; /*YYYYMM format */
74 %let mon_var = %sysfunc(inputn(putn(&score_month,6.)!!'01',yymmdd8.)); /*converting to sas date format by adding day as
74 ! 01 */
WARNING: Argument 1 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set
to a missing value.
75 %let last_mon = %sysfunc(intnx(month,&mon_var,-1),yymmn6.); /*subtracting 1 month */
76 %put &score_month***&last_mon***;
201910***.***
77
78 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
90
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Maxim 28 ... do not format macro variables for the purpose of performing arithmetic or logical operations.

 

%let score_month = 201910; 
%let mon_var = %sysfunc(inputn(&score_month,yymmn6.)); /* Not formatted */
%let last_mon = %sysfunc(intnx(month,&mon_var,-1)); /* Not formatted */
%put &=score_month &=last_mon;

Now to see what the integer in &last_mon represents, so humans can understand it, formatting is appropriate.

 

%put %sysfunc(putn(&last_mon,yymmn6.));

 

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

Maxim 28 ... do not format macro variables for the purpose of performing arithmetic or logical operations.

 

%let score_month = 201910; 
%let mon_var = %sysfunc(inputn(&score_month,yymmn6.)); /* Not formatted */
%let last_mon = %sysfunc(intnx(month,&mon_var,-1)); /* Not formatted */
%put &=score_month &=last_mon;

Now to see what the integer in &last_mon represents, so humans can understand it, formatting is appropriate.

 

%put %sysfunc(putn(&last_mon,yymmn6.));

 

 

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 3548 views
  • 1 like
  • 2 in conversation