Solved
New Contributor
Posts: 4

# how to regress Y against each X separately

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

Accepted Solutions
Solution
‎06-05-2018 09:11 PM
SAS Super FREQ
Posts: 4,245

## Re: how to regress Y against each X separately

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"

All Replies
Super User
Posts: 9,599

## Re: how to regress Y against each X separately

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

...

New Contributor
Posts: 4

## Re: how to regress Y against each X separately

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.
Super User
Posts: 23,773

## Re: how to regress Y against each X separately

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

New Contributor
Posts: 4

## Re: how to regress Y against each X separately

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.

Solution
‎06-05-2018 09:11 PM
SAS Super FREQ
Posts: 4,245

## Re: how to regress Y against each X separately

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"

Super User
Posts: 23,773

## Re: how to regress Y against each X separately

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

SAS Super FREQ
Posts: 4,245

## Re: how to regress Y against each X separately

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

New Contributor
Posts: 4

## Re: how to regress Y against each X separately

Hi Rick, the article helps. Thanks a lot.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 8 replies
• 165 views
• 0 likes
• 4 in conversation