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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 131 views
  • 0 likes
  • 2 in conversation