DATA Step, Macro, Functions and more

Macro %IF fails when variable has minus sign

Reply
Super Contributor
Posts: 316

Macro %IF fails when variable has minus sign

This is a very silly beginner question but would like to seek some advise. I'm getting this error when the value of the macro variable layer has a dash. Seems like it's still trying to evaluate it even I have placed %str.

 

This is the error I get and the code sample below.

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is
required. The condition was: &layer = stg or &layer = stg-m

 

options symbolgen;
%let layer=stg-m;

%macro test;
	%if %str(&layer) = %str(stg) or %str(&layer) = %str(stg-m) %then %do;
		%put CORRECT;
	%end;
%mend;

%test;
Super User
Posts: 5,254

Re: Macro %IF fails when variable has minus sign

I guess that som quoting macro will work, but perhaps a simpler approach would to include single quotes in you macro variable values.
Data never sleeps
Trusted Advisor
Posts: 1,607

Re: Macro %IF fails when variable has minus sign


milts wrote:

This is a very silly beginner question but would like to seek some advise. I'm getting this error when the value of the macro variable layer has a dash. Seems like it's still trying to evaluate it even I have placed %str.

 

This is the error I get and the code sample below.

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is
required. The condition was: &layer = stg or &layer = stg-m

 

options symbolgen;
%let layer=stg-m;

%macro test;
	%if %str(&layer) = %str(stg) or %str(&layer) = %str(stg-m) %then %do;
		%put CORRECT;
	%end;
%mend;

%test;

You need to use %quote around &layer, and you don't need to include stg in the %str() function

 

%if %quote(&layer) = stg or %quote(&layer) = %str(stg-m) %then %do;

 

Super User
Posts: 5,071

Re: Macro %IF fails when variable has minus sign

[ Edited ]

A simple solution is to add double quotes around everything:

 

%if "&layer" = "stg" or "&layer" = "stg-m" %then %do;

 

The double quotes suppress macro language's urge to treat the dash as an instruction to subtract.

 

For this to work, you have to be careful to construct &LAYER with no leading or trailing blanks.

Super User
Posts: 9,662

Re: Macro %IF fails when variable has minus sign

Yeah. minus is a special character for macro facility, you need mask it.

Since it is in a macro variable ,use %bquote() better. %str() is for string .

 

 

options symbolgen;
%let layer=stg-m;

%macro test;
	%if %bquote(&layer) = %str(stg) or %bquote(&layer) = %str(stg-m) %then %do;
		%put CORRECT;
	%end;
%mend;

%test
Super User
Posts: 9,662

Re: Macro %IF fails when variable has minus sign

options symbolgen;
%let layer=stg-m;

%macro test/minoperator mindelimiter=' ';
	%if %bquote(&layer) in %str(stg) %str(stg-m) %then %do;
		%put CORRECT;
	%end;
%mend;

%test
Ask a Question
Discussion stats
  • 5 replies
  • 119 views
  • 3 likes
  • 5 in conversation