BookmarkSubscribeRSS Feed
milts
Pyrite | Level 9

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;
5 REPLIES 5
LinusH
Tourmaline | Level 20
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
PaigeMiller
Diamond | Level 26

@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;

 

--
Paige Miller
Astounding
PROC Star

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.

Ksharp
Super User

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
Ksharp
Super User
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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 3602 views
  • 3 likes
  • 5 in conversation