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

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.5153
10.5271
5326
12464
13542
1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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"

View solution in original post

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

...

daviddu
Calcite | Level 5
Hi RW9, your method is what I did previously. The problem is that I have to use the maceo for 3 times, which will be tedious if there are many variable. I think Rick provides a better solution. Thank you very much.
Reeza
Super User

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:

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-run-multiple-regres...

 


@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

 

daviddu
Calcite | Level 5

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.

Rick_SAS
SAS Super FREQ

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"

Reeza
Super User

@Rick_SAS I updated my library article to point to yours now since it's more detailed 🙂

Rick_SAS
SAS Super FREQ

OK, thanks. Small correction: "SAS" didn't publish a blog post, I did. I do not speak for SAS.

daviddu
Calcite | Level 5

Hi Rick, the article helps. Thanks a lot.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 8 replies
  • 892 views
  • 0 likes
  • 4 in conversation