BookmarkSubscribeRSS Feed
bmm0628
Calcite | Level 5

I am trying to run a whole bunch of regressions at once, following the code laid out from https://blogs.sas.com/content/iml/2017/02/13/run-1000-regressions.html

I tried to follow this with my data, and data set, but I have fallen into a problem, and I don't know how to fix it. I'll paste my code below:

%macro DepVar (data=work.import,137);
...
%do i=1 %to &137;  						
		proc reg data=WORK.IMPORT noprint; 	
		outest= PE (rename=(x&i=137));
		model y=x&i; 
	quit;
%end;
%mend;

My dependent variable is titled "DepVar" and there are 137 independent variables in my data set.

 

Thank you!

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Macros must produce legal valid working SAS code after you replace the macro variables with their values.

 

When you replace &i with the value of variable &i, then this line

 

outest= PE (rename=(x&i=137));

when &i=1 turns into

 

outest= PE (rename=(x1=137));

What is wrong with that? Can you see why this is not valid working SAS code?

 

I'd offer suggestions about how to fix it, but it's extremely unclear to me what this line is supposed to be doing.

--
Paige Miller
bmm0628
Calcite | Level 5

@PaigeMiller Thanks for the response. To be totally honest, I am not sure either. From the article that I was looking at, it says "The OUTEST= option saves the parameter estimates in a data set" so that would be the goal.

PaigeMiller
Diamond | Level 26

What is the problem with the rename portion? Where does basic SAS syntax fail in the rename part (which is where you macro variable is used)?? I am implying that you must be able to figure out and fix basic SAS syntax errors if you are going to use macros.

 

I don't understand why you need a rename, or what you are trying to do with the rename, so I cannot suggest an alternative.


Why even use a macro here, when the link you provided gives a non-macro version of the code (and claims it is more efficient)?

--
Paige Miller
Tom
Super User Tom
Super User

The %MACRO statement has mistakes.

%macro DepVar (data=work.import,137);

All parameters that allow passing the values by position (as opposed to by name) have to come FIRST.  So you cannot have the named parameter DATA= listed first and the positional one second.

You cannot start a parameter's name with digit 1.  SAS names must start with a letter or an underscore.

 

The SAS code the macro is trying to generate also looks strange.  First get your SAS code to work for one dependent variable.  Then you can try using a macro to generate that code.

 

So if you dependent variable is named DEPVAR then it should be to left of the equal sign in the MODEL statement.  If you have 137 independent variables why not just put them all into the model to begin with?

model depvar= indep1 - indep137 ;

 

 

ballardw
Super User

Macro variable names cannot start with a digit: 137 is invalid.

You should have gotten something like this in your log:

2    %macro DepVar (data=work.import,137);
ERROR: Invalid macro parameter name 137.  It should be a valid SAS identifier no longer than 32
       characters.
ERROR: A dummy macro will be compiled.
3       %put &data &137;
4    %mend;

So you need a NAME

%macro DepVar (data=work.import, limit=);

Second, if you are going to define a macro parameter that is not a keyword, like limit=, it must appear before any of the keyword parameters.

 

Then use %do i= 1 %to &limit;

 

Third this is just plain wrong.

(rename=(x&i=137));

Data set variable names cannot start with a digit. If you want to use name literals that would have to be "137"n but that requires having option validvarname=any set.

 

You have missed the point of the example that used the macro approach.  The macro was provided to show a typical macro approach to motivate why it is less efficient than reshaping the data and then using BY processing. Please reread the article.

data_null__
Jade | Level 19

Did no notice where the author says don't do it that way?

 

If you use a macro loop to do this computation, it will take a long time for all the reasons stated in the article "The slow way or the BY way." Fortunately, there is a more efficient alternative.

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
  • 7 replies
  • 1002 views
  • 0 likes
  • 6 in conversation