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

I have 20 variables in the same data file. I need to try to run regressions with var0 being the dependent variable, and the independent variables being any five variables out of the rest 19 variables. Is there any quick way to run all the possible regressions in this case?

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

Hi.  New situation, six variables, VAR1 is dependent.  How many regressions taking VAR2-VAR6 three variables at a time ... 10 combinations ...

 

var2 var3 var4
var2 var3 var5
var2 var3 var6
var2 var4 var5
var2 var4 var6
var2 var5 var6
var3 var4 var5
var3 var4 var6
var3 var5 var6

 

Given your 19 variables taken 5 at a time, that's 11,628 regressions.  You can write a macro (no problem, see below), but do you really want to " ... run all the possible regressions ... " ?

 

* generate all combinations of 5 vars taken 3 at a time;

data vars (keep=v1-v3);
array v(2:6) $4 ('var2' 'var3' 'var4' 'var5' 'var6');
ncomb=comb(5,3);
do j=1 to ncomb;
   call lexcomb(j, 3, of v(*));
   put (v1-v3) ($5.);
   output;
end;

* macro variable with total number of combinations, 5 vars taken 3 at a time;
call symputx('regs',ncomb);
run;

 

* read combination, run regeression;

%macro reg;
%do j=1 %to &regs;
data _null_;
  rec=&j;
  set vars point=rec;
  call symput('indpt',catx(' ',of v1-v3));
  stop;
run;

 

proc reg data=x;
model var1 = &indpt;
run;

quit;
%end;
%mend;

 

 

 

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

I suppose you could write a macro to do this, but that doesn't sound like a trivial thing to do; and anyway, using Partial Least Squares regression (PROC PLS) ought to produce superior results (lower mean squared error for coefficients and predicted values) than using PROC REG as you are trying to do.

--
Paige Miller
lizzy28
Quartz | Level 8

I tend to write a macro to do this because I have to utimately run Vector Autoregression (proc varmax), but don't really know how to start.

 

Thank you!

 

Lizi

PGStats
Opal | Level 21

Sounds like you want to select 5 variables for your model. If you want the selection to be based on r-square, adjusted r-square or Mallows CP then proc reg can do the search for you. Something like

 

proc reg data=myData;
model x0 = x1-x20 / selection=CP start=5 stop=5 best=100;
run;

will find the 100 best 5-variable linear models according to the CP criteria.

PG
PaigeMiller
Diamond | Level 26

Okay, that works, but I still think this is a poor choice of analysis.

--
Paige Miller
PGStats
Opal | Level 21

Agreed, statistical procedures for variable selection should never be used blindly. Simulations and sensitivity analyses have shown their instability over and over.

On model selection issues, read http://support.sas.com/documentation/cdl/en/statug/68162/HTML/default/viewer.htm#statug_glmselect_de...

 

PG
PaigeMiller
Diamond | Level 26

I'm sorry if I am sounding like a cantakerous old model builder, I agree that the problems using variable selection techniques are well documented, and I agree that you shouldn't use these procedures blindly, but the original problem as stated tries to use these problematic model building procedures blindly with silly and meaningless restrictions (model can have only five variables).

--
Paige Miller
SteveDenham
Jade | Level 19

I want to nominate @PaigeMiller's response for the Hall of Fame, if we had one.  To quote John Tukey twice:

 

There is no point in being precise when you don't know what you're talking about.

 

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.

 

Steve Denham

MikeZdeb
Rhodochrosite | Level 12

Hi.  New situation, six variables, VAR1 is dependent.  How many regressions taking VAR2-VAR6 three variables at a time ... 10 combinations ...

 

var2 var3 var4
var2 var3 var5
var2 var3 var6
var2 var4 var5
var2 var4 var6
var2 var5 var6
var3 var4 var5
var3 var4 var6
var3 var5 var6

 

Given your 19 variables taken 5 at a time, that's 11,628 regressions.  You can write a macro (no problem, see below), but do you really want to " ... run all the possible regressions ... " ?

 

* generate all combinations of 5 vars taken 3 at a time;

data vars (keep=v1-v3);
array v(2:6) $4 ('var2' 'var3' 'var4' 'var5' 'var6');
ncomb=comb(5,3);
do j=1 to ncomb;
   call lexcomb(j, 3, of v(*));
   put (v1-v3) ($5.);
   output;
end;

* macro variable with total number of combinations, 5 vars taken 3 at a time;
call symputx('regs',ncomb);
run;

 

* read combination, run regeression;

%macro reg;
%do j=1 %to &regs;
data _null_;
  rec=&j;
  set vars point=rec;
  call symput('indpt',catx(' ',of v1-v3));
  stop;
run;

 

proc reg data=x;
model var1 = &indpt;
run;

quit;
%end;
%mend;

 

 

 

lizzy28
Quartz | Level 8

Thanks a lot, Mike!

 

I know it's unsual to "run all possible regressions", but this will allow me to exclude those with higher forecast errors.

 

Thanks again,

Lizi

PaigeMiller
Diamond | Level 26

Just because something can be programmed, it doesn't mean that it produces a good result.

 

In particular, the models that wind up with higher forecast errors may be due to lots of reasons, including multi-collinearity among the X-variables, and in any event @PGStats has provided a link that explains why this type of model selection is misleading at best, and should be avoided "because it violates every principle of statistical estimation and hypothesis testing".

 

So I'll say it again, your restriction of having exactly 5 independent variables in the model is silly and meaningless and most likely misleading; and again I recommend PROC PLS on all 19 independent variables.

--
Paige Miller

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 4125 views
  • 3 likes
  • 5 in conversation