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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 655 views
  • 2 likes
  • 3 in conversation