BookmarkSubscribeRSS Feed
kampong
Calcite | Level 5

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?

2 REPLIES 2
Tom
Super User Tom
Super User

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;
Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 783 views
  • 2 likes
  • 3 in conversation