Solved
Contributor
Posts: 23

# Combination macro for Regression with a twist

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

Accepted Solutions
Solution
‎10-30-2014 12:39 PM
Posts: 1,270

## Re: Combination macro for Regression with a twist

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;

All Replies
Super User
Posts: 13,583

## Re: Combination macro for Regression with a twist

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?

Contributor
Posts: 23

## Re: Combination macro for Regression with a twist

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

Super User
Posts: 9,599

## Re: Combination macro for Regression with a twist

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;

Super User
Posts: 13,583

## Re: Combination macro for Regression with a twist

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;

Contributor
Posts: 23

## Re: Combination macro for Regression with a twist

Hi,

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,

Solution
‎10-30-2014 12:39 PM
Posts: 1,270

## Re: Combination macro for Regression with a twist

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;

Contributor
Posts: 23

## Re: Combination macro for Regression with a twist

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

Posts: 1,270

## Re: Combination macro for Regression with a twist

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

Super User
Posts: 10,784

## Re: Combination macro for Regression with a twist

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

🔒 This topic is solved and locked.