DATA Step, Macro, Functions and more

Loop a macro for each combination of two lists

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Loop a macro for each combination of two lists

I have a macro which i need run for each combination of 2 lists. For example list1 = old young , list2 = small medium large. I want the macro to run for each combination of the 2 lists (i.e. a Cartesian product).

Is there a macro that someone has created which i can wrap around my inner macro, and loop it for each combination of the two lists.

I have seen macros like %ITERLISTI which loop nicely for each value of a single list, but i need to extend it to two lists.


Thanks


Accepted Solutions
Solution
‎04-10-2015 10:55 AM
Super User
Posts: 5,503

Re: Loop a macro for each combination of two lists

The basic structure here assumes that default delimiters of the %SCAN function can be used:

%macro two_lists (list1=, list2=);

%local i j next_i next_j;

  %do i=1 %to %sysfunc(countw(&list1));

     %let next_i = %scan(&list1, &i);

     %do j=1 %to %sysfunc(countw(&list2));

        %let next_j = %scan(&list2, &j);

        %*** DO something here like call a macro;

     %end;

  %end;

%mend two_lists;

Good luck.

View solution in original post


All Replies
Super User
Super User
Posts: 7,949

Re: Loop a macro for each combination of two lists

You would need to put more details about what you are trying to do.  You could for instance do:

data _null_;

     do i="old young","small medium large";

          call execute('%your_macro (the_list='||strip(i)||');');

     end;

run;

Solution
‎04-10-2015 10:55 AM
Super User
Posts: 5,503

Re: Loop a macro for each combination of two lists

The basic structure here assumes that default delimiters of the %SCAN function can be used:

%macro two_lists (list1=, list2=);

%local i j next_i next_j;

  %do i=1 %to %sysfunc(countw(&list1));

     %let next_i = %scan(&list1, &i);

     %do j=1 %to %sysfunc(countw(&list2));

        %let next_j = %scan(&list2, &j);

        %*** DO something here like call a macro;

     %end;

  %end;

%mend two_lists;

Good luck.

New Contributor
Posts: 3

Re: Loop a macro for each combination of two lists

Posted in reply to Astounding

Works perfectly, thanks

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 249 views
  • 1 like
  • 3 in conversation