Suppose the data structure is as follows. Now I want to regress y in respect to each x separately. I can do it in three steps, but I want to ask whether I could do it in a more easy way, say using a loop or macro? Could anyone provide me some sample code? Thanks a lot.
Y | X1 | X2 | X3 |
5.5 | 1 | 5 | 3 |
10.5 | 2 | 7 | 1 |
5 | 3 | 2 | 6 |
12 | 4 | 6 | 4 |
13 | 5 | 4 | 2 |
Sure. Sample code, example, and step-by-step discussion of this problem are in the article "An easy way to run thousands of regressions in SAS"
What does your regress code look like? You can do it in macro:
%macro Do_Regress (x_var=); ... %mend Do_Regress; %Do_Regress (x_var=x1); %Do_Regress (x_var=x2); %Do_Regress (x_var=x3);
But there may be simpler ways, for instance if you normalise the data, go down rather than across, you can likely do it in one simple step, so a dataset like:
Y X RES
5.5 1 1
5.5 2 5
...
The easiest way is to transform your data into a long format and use PROC REG with a BY statement. You can use PROC TRANSPOSE to flip the data and then a single PROC REG.
See some more details here:
@daviddu wrote:
Suppose the data structure is as follows. Now I want to regress y in respect to each x separately. I can do it in three steps, but I want to ask whether I could do it in a more easy way, say using a loop or macro? Could anyone provide me some sample code? Thanks a lot.
Y X1 X2 X3 5.5 1 5 3 10.5 2 7 1 5 3 2 6 12 4 6 4 13 5 4 2
Hi Reeza, your suggestion also helps. The proc transpose is new to me. I have to check your idea in more details. Thanks a lot.
Sure. Sample code, example, and step-by-step discussion of this problem are in the article "An easy way to run thousands of regressions in SAS"
@Rick_SAS I updated my library article to point to yours now since it's more detailed 🙂
OK, thanks. Small correction: "SAS" didn't publish a blog post, I did. I do not speak for SAS.
Hi Rick, the article helps. Thanks a lot.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.