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

Hi All,

I have seen references for combination macros where you can consistently select any 2 combination output with 4 options possible. But if I want to automatically select every 1 to n (in this case n=4) combination.

data auto;

     input y x1 x2 x3 x4;

     datalines;

a 1 2 3 5

f 2 3 2 4

;

run;

What if I want to select variables x1-x4 such that following combinations are generated:

1. x1

2. x1 x2

3. x1 x2 x3

4. x1 x2 x3 x4

5. x4 x3 x2

6. x3 x2

.....and so on

Have you guys come across this problem? I want to use it in proc model to generate output for any possible combination of indpendent variables.

Cheers

Jane

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

Hi,

You can use start and stop options to get all possible combination of indpendent variables.

proc reg data=have;

   model y=x1 x2 x3 x4/selection=rsquare start=1 stop=4 stb adjrsq cp mse sse;

run;

quit;

View solution in original post

9 REPLIES 9
ballardw
Super User

Are you trying to build model statements for proc reg or select variables in a dataset?

Is the ORDER of the result going to matter at all?

Jane7476
Calcite | Level 5

Hi,

I am trying to select variables for the model.

The order isn't important.

If I were to write the model statement manually then it would be something like below for dataset auto:

1. model y = x1 x2 x3 x4;

2. model y = x1 x4;

3. model y= x2 x3 x4;

....etc.

Thanks

RW9
Diamond | Level 26 RW9
Diamond | Level 26

If you just want those combinations then a do loop can generate it (you could then use that to generate you model lines):

data want;

  length mod $200;

  retain mod;

  do i=1 to 4;

    do j=i to i+3;

      test=j;

      if test > 4 then test=test-4;

      mod=catx(' ',strip(mod),'x'||put(test,1.));

      output;

    end;

    mod='';

  end;

run;

ballardw
Super User

I think that this might be a better fit:

This datastep generates macro variables as well as a data set you can examine before going to the next part.

data work.junk;
   array x[4] $10 _temporary_ ('Var1' 'Var2' 'Var3' 'Var4');  /* replace 10 with length of the longest variable name*/
   length longstring $ 200; /* replace 200 with a value long enough to hold all of the variables + one space for each variable*/
   length name $ 15;
   _n_=dim(x);
   do _k_ = 1 to 4;
      ncomb= comb(_n_,_k_);

      do _j_ = 1 to ncomb;
         longstring = '';
         call allcomb(_j_,_k_, of x

  • );
             do _z_= 1 to _k_;
                longstring = catx(' ',longstring,x[_z_]);
             end;
             counter + 1;
             c= put(counter,best8.);
             Name = cats('Comb',c);
             output;
          end;
       end;
       call symputx('CombCount',C);
    run;
  • And use that in a Macro

    %macro myreg;

         proc reg <options>;

         <other statements>

         %do I = 1 to &CombCount;

              model y = &&comb&i ;

         %end;

         run;

    %mend;

    %myreg;

    Jane7476
    Calcite | Level 5

    Hi,

    Thanks for the reply.

    Unfortunately can't use the code as 'allcomb' seems to be a sub routinue introduced in 9.2 for what i understand. I have 9.1.

    Regards,


    stat_sas
    Ammonite | Level 13

    Hi,

    You can use start and stop options to get all possible combination of indpendent variables.

    proc reg data=have;

       model y=x1 x2 x3 x4/selection=rsquare start=1 stop=4 stb adjrsq cp mse sse;

    run;

    quit;

    Jane7476
    Calcite | Level 5

    Hi

    Very interesting use of start and stop. Didn't knew these options existed.

    Given the output I am assuming the model selection methodology is based on C(p), MSE and SSE.

    Is there a way to populate the paramater estimates for the model combinations that haven't been selected as final models. It would be interesting to see how the coefficients change with respect to other non-significant variables.


    Thanks

    stat_sas
    Ammonite | Level 13

    Model selection is based on rsquare. This will give you parameter estimates for the model having highest rsquare.

    Ksharp
    Super User

    you can use proc summary to get all of combination of them.

    data auto;
         input y x1 x2 x3 x4;
         datalines;
    3 1 2 3 5
    3 2 3 2 4
    ;
    run;
    data temp;
     set sashelp.vcolumn(where=(libname='WORK' and memname='AUTO' and name like 'x%'));
     keep name;
     run;
     proc transpose data=temp out=x(drop=_:);var name;run;
     proc summary data=x ;
     class col:;
     output out=y;
    run;
    data want;
     set y;
     combine=catx(' ',of col:);
     keep combine;
    run;
    

    Xia Keshan

    Ready to join fellow brilliant minds for the SAS Hackathon?

    Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

    Register today!
    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
    • 9 replies
    • 2667 views
    • 8 likes
    • 5 in conversation