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
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;
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?
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
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;
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
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;
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,
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;
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
Model selection is based on rsquare. This will give you parameter estimates for the model having highest rsquare.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.