BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
catch18
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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 
--
Paige Miller

View solution in original post

8 REPLIES 8
ed_sas_member
Meteorite | Level 14

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,

catch18
Obsidian | Level 7

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.

ed_sas_member
Meteorite | Level 14

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)

catch18
Obsidian | Level 7

Many thanks!

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
PaigeMiller
Diamond | Level 26

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 
--
Paige Miller
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
catch18
Obsidian | Level 7

Thank you!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1128 views
  • 0 likes
  • 3 in conversation