Programming the statistical procedures from SAS

Proc reg, output statement with multiple models

Reply
New Contributor
Posts: 4

Proc reg, output statement with multiple models

Hi,
I am computing mutliple simple linear regressions in the same procedure 'proc reg'.
The issue I have is that in the outest I have my 2 regressions with intercept, Rsq ... etc
BUT in the output dataset generated with 'output out=' statement (raw data), I can't have residuals for the first model And for the second model :

Is anyone can help on that ?

proc reg data = htwt outest=est1 rsquare;
eq1: model weight=height;
eq2: model weight=height age;
output out = htwt_2 r=res;
run;quit;


data htwt;
input sex $ age :3.1 height weight @@;
datalines;
f 143 56.3 85.0 f 155 62.3 105.0 f 153 63.3 108.0 f 161 59.0 92.0
f 191 62.5 112.5 f 171 62.5 112.0 f 185 59.0 104.0 f 142 56.5 69.0
f 160 62.0 94.5 f 140 53.8 68.5 f 139 61.5 104.0 f 178 61.5 103.5
f 157 64.5 123.5 f 149 58.3 93.0 f 143 51.3 50.5 f 145 58.8 89.0
m 164 66.5 112.0 m 189 65.0 114.0 m 164 61.5 140.0 m 167 62.0 107.5
m 151 59.3 87.0
;

/* data source */
http://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_reg_sect056....

Thanks !
Super Contributor
Posts: 281

Re: Proc reg, output statement with multiple models

Add an OUTPUT statement under EQ1
New Contributor
Posts: 4

Re: Proc reg, output statement with multiple models

Ok,
Thank you, that makes almost the job.
This code outputs 2 datasets that contains the intial data and the residual column.

The thing is that I am not able to output in the same dataset !
If i try to output in the same dataset i got an error : "ERROR: Data set WORK.HTWT_1 is already open for output."

proc reg data = htwt outest=est1 rsquare;
eq1: model weight=height;
output out = htwt_1 r=res1;
eq2: model weight=height age;
output out = htwt_2 r=res2;
run;quit;

Regards,
Antoine
Super Contributor
Posts: 281

Re: Proc reg, output statement with multiple models

> The thing is that I am not able to output in the same
> dataset !

I don't think this is possible from PROC REG.

If only SAS provided a way to MERGE data sets! That would solve the problem! Message was edited by: Paige
New Contributor
Posts: 4

Re: Proc reg, output statement with multiple models

My conclusion is that with proc REG I MUST ouptput in different datasets to get residuals from different models !
(I don't know if the output logic is different in proc glm or other regression procedure)

So I have a solution but which is probably not optimal :

/* add a raw id to my input data */
data Htwt;
set Htwt;
RAW_ID = _N_;
run;

/* run my 2 regressions and output residuals */
proc reg data = htwt outest=est1 rsquare;
eq1: model weight=height;
output out = htwt_1 r=res1;
eq2: model weight=height age;
output out = htwt_2 r=res2;
run;quit;

/* merge back together residuals */
proc sort data = htwt_1; by RAW_ID; run;
proc sort data = htwt_2; by RAW_ID; run;
data htwt_1_2(drop = RAW_ID);
merge htwt_1 htwt_2;
by raw_id;
run;

At the end it may be same processing time than running two time the PROC REG :
proc reg data = htwt outest=est1 rsquare;
eq1: model weight=height;
output out = htwt_out r=res1;
run;quit;
proc reg data = htwt_out outest=est1 rsquare;
eq2: model weight=height age;
output out = htwt_out r=res2;
run;quit;

Regards,
Super Contributor
Posts: 281

Re: Proc reg, output statement with multiple models

Sorting is unnecessary, as is the step containing RAW_ID = _N_;

Both output data sets are in the exact same sequence, and so you can simply do the merge without the BY statement here.
Ask a Question
Discussion stats
  • 5 replies
  • 782 views
  • 0 likes
  • 2 in conversation