I am doing an analysis on data for 3 specific months: 202006, 202007, 202008.
And for each of these 3 months, I want a macro loop, which will run only for M-1 and M+6 month where M takes the above three months as values. The starting part of my macro is this:
%LET Mon_ST = 202006;
%LET Mon_EN = 202008;
Option obs = Max;
%macro copy_data();
%DO Mnth = &Mon_ST. %TO &Mon_EN. ;
%IF %SUBSTR(&Mnth.,5,2) = 13 %THEN %LET Mnth = %EVAL(%SUBSTR(&Mnth.,1,4)+1)01;
%Put Mnth = &Mnth.;
Now, I want to create another loop within this macro which will run for only two specific time periods for each of these months. For eaxample, when Mnth =202006, the loop will create another macro variable, say MON, which will first take value 202005 (Mnth-1) and on the next iteration will take value 202012 (Mnth+6). Similary, when Mnth =202007, MON will first be equal to 202006, and then 202101.
How do I create this sub loop? Any help will be much appreciated. Thanks!
One way:
%let mon_st = 202006;
%let mon_en = 202008;
option obs = max;
%macro copy_data();
%local mnth from to by m;
%do mnth = &mon_st. %to &mon_en. ;
%if %substr(&mnth.,5,2) = 13 %then %let mnth = %eval(&mnth+100-12);
%put &=mnth;
%let from= %sysfunc(intnx(month,%sysfunc(inputn(&mnth,yymmn6.)),-1),yymmn6.);
%let to = %sysfunc(intnx(month,%sysfunc(inputn(&mnth,yymmn6.)),+6),yymmn6.);
%let by = %eval(&to - &from);
%do m=&from %to &to %by &by;
%put &=m;
%end;
%end;
%mend;
%copy_data;
MNTH=202006
M=202005
M=202012
MNTH=202007
M=202006
M=202101
MNTH=202008
M=202007
M=202102
One way:
%let mon_st = 202006;
%let mon_en = 202008;
option obs = max;
%macro copy_data();
%local mnth from to by m;
%do mnth = &mon_st. %to &mon_en. ;
%if %substr(&mnth.,5,2) = 13 %then %let mnth = %eval(&mnth+100-12);
%put &=mnth;
%let from= %sysfunc(intnx(month,%sysfunc(inputn(&mnth,yymmn6.)),-1),yymmn6.);
%let to = %sysfunc(intnx(month,%sysfunc(inputn(&mnth,yymmn6.)),+6),yymmn6.);
%let by = %eval(&to - &from);
%do m=&from %to &to %by &by;
%put &=m;
%end;
%end;
%mend;
%copy_data;
MNTH=202006
M=202005
M=202012
MNTH=202007
M=202006
M=202101
MNTH=202008
M=202007
M=202102
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.