DATA Step, Macro, Functions and more

Use %scan and tranwrd?

Reply
Frequent Contributor
Posts: 107

Use %scan and tranwrd?

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.

Super User
Posts: 19,770

Re: Use %scan and tranwrd?

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. 

Super User
Posts: 10,020

Re: Use %scan and tranwrd?


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


Respected Advisor
Posts: 3,799

Re: Use %scan and tranwrd?

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

Ask a Question
Discussion stats
  • 3 replies
  • 250 views
  • 2 likes
  • 4 in conversation