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***
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.));
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.));
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.