Solved
New Contributor
Posts: 4

Looping SAS regreression for two variables

Hi,

I am running a regression where I need to pass two variables at a time and check all the combinations.

I have 10 macroeconomic variables and would like to run 45 (10C2) regression.Please suggest some solution by creating a long dataset so that I can leverage 'by' funcitionality and have all the results in one data set.

Thanks,

Rajat

Accepted Solutions
Solution
‎05-08-2018 04:09 PM
Super User
Posts: 13,508

Re: Looping SAS regreression for two variables

[ Edited ]

Is something like this what you are looking for?

```data junk;
set sashelp.class;
varlist= 'age height weight';
length firstvar secondvar \$ 15;
do i= 1 to (countw(varlist)-1);
firstvar= scan(varlist,i);
do j= (i+1) to countw(varlist);
secondvar = scan(varlist,j);
output;
end;
end;
run;

proc sort data=junk;
by firstvar secondvar;
run;```

I used a somewhat smaller example set with 3 variables instead of 10.

If your actual variables have names longer than 15 character adjust the length statement to match your longest.

Remember that BY group processing can use more than a single variable, just like the sort.

All Replies
PROC Star
Posts: 1,279

[ Edited ]

See this

An easy way to run thousands of regressions in SAS

New Contributor
Posts: 4

Re: Looping SAS regreression for two variables

This is very popular page but solution is good only for 1 variable

PROC Star
Posts: 1,279

Re: Looping SAS regreression for two variables

Can you show us some example of what your data looks like?

Solution
‎05-08-2018 04:09 PM
Super User
Posts: 13,508

Re: Looping SAS regreression for two variables

[ Edited ]

Is something like this what you are looking for?

```data junk;
set sashelp.class;
varlist= 'age height weight';
length firstvar secondvar \$ 15;
do i= 1 to (countw(varlist)-1);
firstvar= scan(varlist,i);
do j= (i+1) to countw(varlist);
secondvar = scan(varlist,j);
output;
end;
end;
run;

proc sort data=junk;
by firstvar secondvar;
run;```

I used a somewhat smaller example set with 3 variables instead of 10.

If your actual variables have names longer than 15 character adjust the length statement to match your longest.

Remember that BY group processing can use more than a single variable, just like the sort.

New Contributor
Posts: 4

Re: Looping SAS regreression for two variables

This helped ..Thanks!

Posts: 2,991

Re: Looping SAS regreression for two variables

Of course, just because you CAN get SAS to do these 45 regressions, that doesn't mean you SHOULD do it this way. There are better solutions than fitting the 45 different models. One of them is Partial Least Squares regression, which can handle all 10 variables in one model and not get badly influenced by collinearity between the variables.

--
Paige Miller
New Contributor
Posts: 4

Re: Looping SAS regreression for two variables

I amusing fractional response model.Avoided that part just to keep the
things simple...Agree that it is not very prudent to check each possible
combination

Posts: 2,991

Re: Looping SAS regreression for two variables

I don't know what a fractional response model is, but any modelling that can be handled by regression can be handled by Partial Least Squares. As I said, PLS will usually fit the model better, and avoids these difficulties of looping throught 45 combinations of variables.

--
Paige Miller
☑ This topic is solved.