Q:

SAS macro nested loop

I would like to build a logistic model containing two predictors. One from set all_indeps1, and one from all_indeps2. I run below macro, however, it only run model with the first variable from all_indeps1, and all variables from all_indeps2. How should I fix the macro so that I could have all possible combinations of two variables from two sets?
Also, I would like to only output the p-values for each predictors from the logistic model, any ideas?
Many Thanks!
%macro trivariate(all_indeps1, all_indeps2);
%let k = 1;
%let l = 1;
%let indep1 = %scan(&all_indeps1, &k);
%let indep2 = %scan(&all_indeps2, &l);

    %do %while("&indep1" NE "");
        %do %while ("&indep2" NE "");
    title "independent variable is &Indep1 and &Indep2";
    proc logistic data = A descending;
        model Y = &indep1 &indep2;
    run;
        %let l = %eval(&l + 1);
        %let indep2 = %scan(&all_indeps2, &l);
        %end;
    %let k = %eval(&k + 1);
    %let indep1 = %scan(&all_indeps1, &k );

    %end;

%mend;

A:

I wouldn't code this as a macro loop at all, but instead set it up so your macro is just the inner bit, and call the macro n1*n2 times.
Say you have two datasets, indep1 and indep2, each of which contains a single column with one variable name per row.  Then if you had a macro:
%macro trivariate(indep1,indep2);
   title "independent variable is &Indep1 and &Indep2";
    proc logistic data = A descending;
        model Y = &indep1 &indep2;
    run;
%mend trivariate;

proc sql;
 select cats('%trivariate(',indep1.var,',',indep2.var,')') into :trivarlist
  separated by ' ' 
  from indep1, indep2;
quit;

&trivarlist.;

It's almost always easier to control repetition outside of the macro language than inside of it, excepting very simple cases; and it's a better programming style as it makes for more portable and reusable code.

