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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2701 views
  • 3 likes
  • 5 in conversation