BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
amsbam1
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Astounding
PROC Star

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.

amsbam1
Calcite | Level 5

Works perfectly, thanks

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
  • 3 replies
  • 1789 views
  • 1 like
  • 3 in conversation