Hey guys, I keep getting error while I try to run my macro in a do loop.
%MACRO MYLOOP;
%DO I=-12 %TO -6;
%LET CM_DATE=%SYSFUNC(INTNX(MONTH,%SYSFUNC(TODAY())-1,&I.,E),DATE7.);
%LET LM_DATE=%SYSFUNC(INTNX(MONTH,%SYSFUNC(TODAY())-1,&I.-1,E),DATE7.);
%LET CM_BG=%SYSFUNC(INTNX(MONTH,%SYSFUNC(TODAY())-1,&I.,B),DATE7.);
DATA D_&LM_DATE.;
INFILE DATALINES DELIMITER=',';
LENGTH BRANCH_CODE $20. PRODUCT_FLG $30. CUST_REMOVE_FLG $11.;
FORMAT BRANCH_CODE $20. PRODUCT_FLG $30. CUST_REMOVE_FLG $11.;
INPUT BRANCH_CODE $ COUNT PRODUCT_FLG $ CUST_REMOVE_FLG $ A_BALANCE Daily_AVG_Balance;
DATALINES;
9000,0,SA ROYALE,D.NORMAL,0,0
9000,0,CA ROYALE,D.NORMAL,0,0
9000,0,SA NTB,D.NORMAL,0,0
9000,0,SA PLATINUM,D.NORMAL,0,0
9000,0,CA MAX,D.NORMAL,0,0
9000,0,CA NTB,D.NORMAL,0,0
9000,0,CA POWER,D.NORMAL,0,0
9000,0,CA PLATINUM,D.NORMAL,0,0
;
%END;
%MEND MYLOOP;
%MYLOOP;
For your example, the data are the same for each iteration of the macro, so you can create the data outside the loop and use a SET statement inside the loop:
DATA D;
INFILE DATALINES DELIMITER=',';
LENGTH BRANCH_CODE $20. PRODUCT_FLG $30. CUST_REMOVE_FLG $11.;
FORMAT BRANCH_CODE $20. PRODUCT_FLG $30. CUST_REMOVE_FLG $11.;
INPUT BRANCH_CODE $ COUNT PRODUCT_FLG $ CUST_REMOVE_FLG $ A_BALANCE Daily_AVG_Balance;
DATALINES;
9000,0,SA ROYALE,D.NORMAL,0,0
9000,0,CA ROYALE,D.NORMAL,0,0
9000,0,SA NTB,D.NORMAL,0,0
9000,0,SA PLATINUM,D.NORMAL,0,0
9000,0,CA MAX,D.NORMAL,0,0
9000,0,CA NTB,D.NORMAL,0,0
9000,0,CA POWER,D.NORMAL,0,0
9000,0,CA PLATINUM,D.NORMAL,0,0
;
%MACRO MYLOOP;
%DO I=-12 %TO -6;
%LET CM_DATE=%SYSFUNC(INTNX(MONTH,%SYSFUNC(TODAY())-1,&I.,E),DATE7.);
%LET LM_DATE=%SYSFUNC(INTNX(MONTH,%SYSFUNC(TODAY())-1,&I.-1,E),DATE7.);
%LET CM_BG=%SYSFUNC(INTNX(MONTH,%SYSFUNC(TODAY())-1,&I.,B),DATE7.);
DATA D_&LM_DATE.;
set D;
run;
%END;
%MEND MYLOOP;
%MYLOOP;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.