Search code examples
sassas-macro

Looping a set of Macros


I'm looking to create a loop such that I run two macros for each dataset

%Let Classification  = Data1 data2 data3 data4; 

%let index  = 1;

%do %until (%Scan(&Classification,&index," ")=);

%Macro1;
%Macro2;

%end;

%let index  = %eval(&Index  + 1);

The problem is my macros are not pre-loaded and are stored in a macro library, is it possible to do this if I run the above as a macro?

Any advice is appreciate in making this loop of macros work

EDIT:

In my ideal situation the loop would run like a macro

%Macro;

where inside it would look like

%Macro Macro;

%let index = 1;

%do %until (%scan(&classification,&index," ")=);

<Lines of Code>

%end;

%let index = %eval(&Index + 1);

%end;

%mend;

Another problem is my macros enclosed in the loop use the &classification to differentiate between data1, data2, data3, data4 as we process through the different lines of code.


Solution

  • It is probably easier to just iterate over the index. Use the countw() function to find how many iterations to do.

    %macro loop(list);
    %local index next ;
    %do index=1 %to %sysfunc(countw(&list,%str( )));
      %let next=%scan(&list,&index,%str( ));
      ... code to process &NEXT ...
    %end;
    %mend ;
    

    Then pass in the list to the macro as the parameter value.

    %Let Classification  = Data1 data2 data3 data4; 
    %loop(&classification);