Hi,
I regressed several response variables defined as &xlist on an independent variable with the code below:
proc reg data=work.'My_data';
model &xlist=E;
output out = cal3_resid r =residual;
run;
&xlist= Var1 var2 var3....;
How do I get r=residual to have a separate name for each variable defined by &xlist?
Thanks.
It would seem to me that some sort of looping is unavoidable in this case.
%macro dothis;
%let xlist= Var1 var2 var3;
%let residlist=;
%do i=1 %to %sysfunc(countw(&xlist));
%let residlist=&residlist %scan(&xlist,&i,%str( ))_r;
%end;
proc reg data=have;
model &xlist=E;
output out = want r =&residlist;
run;
%mend;
%dothis
Hi @catch18
I am not sure that such an option exists. Depending on the number of dependents variables you have, I suggest that you use a macro like this
%macro reg (var);
proc reg data=work.My_data;
model &var.=E;
output out=cal3_resid_&var r=residual;
run;
%mend;
or
proc reg data=work.My_data;
model var1=E;
output out=cal3_resid_var1 r=residual;
model var2=E;
output out=cal3_resid_var2 r=residual;
...
run;
Hope this helps.
Best,
Thanks ed_sas_member.
I have a lot of variables so the second option would involve writing a lot of code.
With your first option, is it intended that I insert my variable names in the first var only?
%macro reg (var1, var2, var3....);
proc reg data=work.My_data;
model &var.=E;
output out=cal3_resid_&var r=residual;
run;
%mend;
Thanks.
Hi @catch18
You're welcome.
With the first option, you can execute it as follows:
proc contents data=work.My_data out=work.listvar (keep=name) noprint;
run;
/* Add a data step to keep only those variables you are interested in*/
data _null;
set listvar;
rc= dosubl(cats('%reg(',name,')'));
run;
It is exactly the same as writing multiple macro call :
%reg(var1)
%reg(var2)
...
%reg(varn)
Many thanks!
The drawback to calling PROC REG in a loop many times (or calling PROC REG many times via CALL EXECUTE or DOSUBL) is that PROC REG then has to pass through the data many times and invert the X'X matrix many times. If you only call PROC REG one time (as in my solution) then you only have to pass through the data once and you only have to invert the X'X matrix once.
It would seem to me that some sort of looping is unavoidable in this case.
%macro dothis;
%let xlist= Var1 var2 var3;
%let residlist=;
%do i=1 %to %sysfunc(countw(&xlist));
%let residlist=&residlist %scan(&xlist,&i,%str( ))_r;
%end;
proc reg data=have;
model &xlist=E;
output out = want r =&residlist;
run;
%mend;
%dothis
You could create another macro variable that has the list of residual names. You could do this in a number of ways, appending _R to the end of the variable names, thus creating a list for the residual variables.
Thank you!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.