BookmarkSubscribeRSS Feed
Solph
Pyrite | Level 9

Hello,

 

I've a dataset with 20 variables; I'd like to have each variable as a dependent variable and the other 19 as independent variables. So in total I'd have 20 regressions. Here is a sample data of 10 cases with just 4 variables x1-x4.

 

data have; input  x1 x2 x3 x4;
datalines;
x1 x2 x3 x4
815 545 40 8699
110 574 22 583
571 604 36 5889
469 594 5 3579
246 622 57 2478
745 652 46 5300
508 525 25 4919
1322 667 33 11576
1576 605 32 7845
1684 661 47 10225

;

 

Here is what I would do if using copying and pasting.

 

proc  reg data=have;  model x1 = x2 x3 x4; run;

proc  reg data=have;  model x2 = x1 x3 x4; run;

proc  reg data=have;  model x3 = x1 x2 x4; run;

proc  reg data=have;  model x4 = x1 x2 x3; run; 

 

I could just do this way but since I need to expand the SAS code to use ODS to output parametric estimates or to output predicted values, etc., I'd very much like to have the code efficient if possible. Someone told me to use %scan and tranwrd. I tried but I guess something was missing, so it didn't work.

 

Any help is appreciated.

3 REPLIES 3
Reeza
Super User

1. Create a dataset with all variable combinations. See Call AllComb

2. Write a macro that a) runs regression, b) captures required data, and c) appends result into master table

3. Use call execute with data set from #1 to execute #2 as required. 

 

I think this methodology is relatively straightforward and there are examples for each step on here. 

 

If you have issues, post your code. 

Ksharp
Super User

Give you an example:



data have; 
input  x1 x2 x3 x4;
datalines; 
815 545 40 8699 
110 574 22 583 
571 604 36 5889 
469 594 5 3579 
246 622 57 2478 
745 652 46 5300 
508 525 25 4919 
1322 667 33 11576 
1576 605 32 7845 
1684 661 47 10225
;
run;

%macro m_reg(vnames=x1 x2 x3 x4);
%do i=1 %to %sysfunc(countw(&vnames,%str( )));
 %let y=%scan(&vnames,&i,%str( ));
 %let x=%sysfunc(tranwrd(&vnames,&y,%str( )));
 
 proc  reg data=have;  model &y = &x; run;
 
%end;
%mend;


%m_reg


data_null__
Jade | Level 19

For one vs. the rest you don't need ALLCOMP and you don't need but one call to PROC REG.

 

data x;
   input  x1-x4;
   datalines; 
815 545 40 8699 
110 574 22 583 
571 604 36 5889 
469 594 5 3579 
246 622 57 2478 
745 652 46 5300 
508 525 25 4919 
1322 667 33 11576 
1576 605 32 7845 
1684 661 47 10225
;;;;
   run;
proc print;
   run;

%macro models(data=x,arg=x1 x2 x3 x4);
   proc reg data=&data outest=est noprint;
   %local i y x;
   %let i = 1;
   %let y = %scan(&arg,&i);
   %do %while(%superq(y) ne);
      %let x = %sysfunc(transtrn(&arg,&y,%str( )));
      &y:model &y = &x;
      run;
      %let i = %eval(&i + 1);
      %let y = %scan(&arg,&i);
      %end;
   %mend;
options mprint=1;
%models;
proc print;
   run;

Capture.PNG

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1312 views
  • 2 likes
  • 4 in conversation