DATA Step, Macro, Functions and more

Syntax Error with a Macro Variable

Reply
Contributor
Posts: 45

Syntax Error with a Macro Variable

[ Edited ]

So I am trying to write a Macro to do backwards selection based on change in estimate(I know SAS has an automatic backwards selection process but it bases it off P-value). I had written a Macro for another project earlier but used proc surveylogistic then, this time I am using proc mixed. I have edited the code and thought that it should work but I am having issues with the interaction effect in proc mixed. When I assign a value to a macro variable inside a macro it doesn't like the * symbol. I get a syntax error, the weird thing is this would work fine outside of a macro. %Let Interaction = A*B; works fine but inside a %Macro if I say %Macro(explanatory = A B A*B) it complains with the error.

Another similar error is when I drop the first variable from the model it drops part of the interaction as well. AKA if I tell it to drop A, it drops the A from A*B so it becomes *B.

 

Outside the macro my code is simply...

 

 

 proc mixed data=Input;
     	class B cat1 cat2 cat3 cat4;
     	model response = A B A*B cat1 cat2 cat3 cat4 explan1 explan2 explan3 /solution;
ods output solutionF = Sol1;
		run;

Inside the macro it is 

proc mixed data=&data;
     	class B &catvar;
     	model  &response = &covars /solution;
	 	ods output solutionF  = Sol&i;
	 run;	

when I get to a part

 

call symput("rcovars", reducedlist);

where Reducedlist is the list of the model after I drop a variable I get

ERROR 22-322: Syntax error, expecting one of the following: a name, ;, (, -, /, :, _ALL_,
_CHARACTER_, _CHAR_, _NUMERIC_.

ERROR 200-322: The symbol is not recognized and will be ignored.

the * is underlined.







Main issue: Why can't we simply make a macro variable with a * sign inside a macro and is there a solution to this? Is there some way to create a variable that counts as A*B aka C or something but not so it is referenced as A*B but rather just C?

Occasional Contributor
Posts: 9

Re: Syntax Error with a Macro Variable

* is a special character and you need to mask it when assigning to a macro variable. Try the macro function %STR to mask *.  Not sure how your "%Let Interaction = A*B;" works, but try to use "%let Interaction=%str(A B A*B);". The same solution for your macro problem.

 

See more details from thel link below 

 

https://v8doc.sas.com/sashtml/macro/z3514str.htm 

 

Hope it can help. 

Super User
Super User
Posts: 6,499

Re: Syntax Error with a Macro Variable

Turn on MPRINT option to see what actual code you macro is generating. There is nothing wrong with what you have posted.

%let data=Input;
%let catvar=cat1 cat2 cat3 cat4 ;
%let response=response;
%let covars=A B A*B cat1 cat2 cat3 cat4 explan1 explan2 explan3 ;
%let i=1 ;

%macro xx ;
proc mixed data=&data;
  class B &catvar;
  model &response = &covars /solution;
ods output solutionF  = Sol&i;
run;	
%mend xx;
options mprint;
%xx;

Which generates

MPRINT(XX):   proc mixed data=Input;
MPRINT(XX):   class B cat1 cat2 cat3 cat4;
MPRINT(XX):   model response = A B A*B cat1 cat2 cat3 cat4 explan1 explan2 explan3 /solution;
MPRINT(XX):   ods output solutionF = Sol1;
MPRINT(XX):   run;

If you macro variables look like they have the right values then perhaps you need to remove any macro quoting so that it is not confusing the SAS parser.

 

Ask a Question
Discussion stats
  • 2 replies
  • 131 views
  • 0 likes
  • 3 in conversation