DATA Step, Macro, Functions and more

Need help in Looping a macro variable

Reply
Senior User
Posts: 1

Need help in Looping a macro variable

Hi everyone, 

 

I am very new in SAS.

I am trying to loop a SAS macro.

 

%let E1 = APPLE;

%let E2 = YAHOO;

%let E3 = SAS;

.....

 

 

%MARCO FILTER_NAME(VAR1=. VAR2=);

proc SQL;

...

%MEND FILTER_NAME;

 

 

%MACRO LOOP;

     %do i=1 %to 3;

     %FILTER_NAME(VAR1=filename_&&E&i, VAR2 = output_filename_&&E&i )  /*trying to pass the variable E1 with name Apple inside here*/

     %end;

%MEND LOOP;

 

 

 

Something is wrong above. Anybody know how?

Super User
Super User
Posts: 6,499

Re: Need help in Looping a macro variable

If you are just starting to learn SAS then you should first concentrate on learning how to write SAS code.  Once you know how to write SAS code you could then begin to learn how to use the SAS macro language to tell SAS how to write the SAS code for you.

 

That being said your program looks basically ok.  There are a couple of typos. But perhaps the main missing thing is that there you never call the macro that you defined.

 

%let E1 = APPLE;
%let E2 = YAHOO;
%let E3 = SAS;

%MACRO FILTER_NAME(VAR1,VAR2);
%put VAR1=&var1;
%put VAR2=&var2;
%MEND FILTER_NAME;
 

%MACRO LOOP;
%do i=1 %to 3;
   %FILTER_NAME(VAR1=filename_&&E&i, VAR2 = output_filename_&&E&i )
%end;
%MEND LOOP;

%loop;
Super User
Posts: 5,080

Re: Need help in Looping a macro variable

While Tom is 100% correct about learning SAS language before learning macro language, here are some secondary considerations.

 

The problem may lie inside the definition of %FILTER_NAME.  Some possibilities:

 

  • SAS names are limited to 32 characters.  Since you are already starting with a string "output_filename_YAHOO" before the macro begins, it is conceivable that the execution of %FILTER_NAME creates a name longer than 32 characters.
  • It is possible that %FILTER_NAME utilizes a macro variable named i.  If that's the case, and there is no %LOCAL statement within %FILTER_NAME, then the interior macro (%FILTER_NAME) will change the value of i that is used to loop in the exterior macro (%LOOP).

If you were to post more of what is inside your macro, you may find you receive more than debugging help ... how to achieve the same result in a cleaner, more efficient way.

Ask a Question
Discussion stats
  • 2 replies
  • 104 views
  • 2 likes
  • 3 in conversation