turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Combination macro for Regression with a twist

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-30-2014 09:33 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Jane7476

10-30-2014 12:39 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Jane7476

10-30-2014 10:58 AM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

10-30-2014 11:39 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Jane7476

10-30-2014 11:53 AM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Jane7476

10-30-2014 12:26 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

10-30-2014 12:30 PM

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,

Solution

10-30-2014
12:39 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Jane7476

10-30-2014 12:39 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to stat_sas

10-30-2014 12:55 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Jane7476

10-30-2014 01:08 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Jane7476

10-31-2014 06:20 AM

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