BookmarkSubscribeRSS Feed
Discaboota
Obsidian | Level 7

Hey guys, I keep getting error while I try to run my macro in a do loop. 

ERROR: The macro MYLOOP generated CARDS (data lines) for the DATA step, which could cause incorrect results. The DATA step and the macro will stop executing.
I read in a few places that I cannot use datalines in a macro definition. I thought of a way to make this into a table and then use the table instead of datalines. 
What can be done here?
%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;
1 REPLY 1
Rick_SAS
SAS Super FREQ

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;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 462 views
  • 0 likes
  • 2 in conversation