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

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
  • 1 reply
  • 4183 views
  • 1 like
  • 2 in conversation