BookmarkSubscribeRSS Feed
XiaoGuaiShou
Obsidian | Level 7

I have this code inside a macro,  I get the error as title shows.

I know the error is caused by the dash(-) in 1st line and underscore(_)  in 2nd line using in SAS Macro. If I commend the 1st and 2nd line out, then the 3rd line work pretty good.

I try to use %str() function to solve it but fail, like 4th 5th line, but I still get error.

 

How to resolve this? Since I do have value with dash(-) and underscore(_) in my data set. 

Any help would be greatly appreciated.

 

Thanks,

 

 

%macro SAS_display;
/*1st line*/%if &value = SAS-A  %then %let valueplay = SAS-A;
/*2nd line*/%if &value = SAS_B %then %let valuedisplay = SAS_B; 
/*3rd line*/%if &value = SAS %then %let programdisplay = SAS; 

/*4th line*/%if &value = %str(SAS%-A) %then %let valueplay = SAS-A; 
/*5th line*/%if &value = %str(SAS%_B) %then %let valuedisp =SAS-b;
%mend SAS_display;

The error when I use 4th line code is:

SYMBOLGEN:  Macro variable PRO resolves to SAS-A
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &pro = SAS%-A 
ERROR: The macro SAS_DISPLAY will stop executing.

 

5 REPLIES 5
WarrenKuhfeld
Rhodochrosite | Level 12

If it were me, and if it were at all possible, I would totally avoid the issue.  I always prefer to write a DATA _NULL_ step where I use SYMGET to retrieve values of a macro variable and then compare them to ordinary quoted strings.  Then I can use SYMPUTX to output the results. This way I can avoid figuring out which macro quoting function I need.  It makes programming so much easier!

ballardw
Super User

Instead of = you may be able to work with IN as in

 

%if %eval(&value. in <something I'm not sure exactly what> ) %then ....

 

But your example doesn't really say much about the actual logic of the problem or why you need to use the %str(SAS%-A) construct in the first place.

 

Also since your log snipped references a macro variable PRO we know the shown code is not what you are executing.

 

 

 

Astounding
PROC Star

A much easier fix:

 

%if "&value" = "SAS-A" %then %let valueplay = SAS-A;

 

Your macro variable &VALUE can't contain any leading or trailing blanks for this to work, however.

Tom
Super User Tom
Super User

The underscore is not an issue because it does not cause the condition to look like arithmetic.

The easiest thing is to add them to all of the comparisons, not just to the ones where the constant is an issue, since the macro variable value could still cause issues.

 

 

%macro SAS_display(value);
%if "&value" = "SAS-A" %then %put VALUE=SAS-A;
%else %if "&value" = "SAS_B" %then %put VALUE=SAS_B;
%else %if "&value" = "SAS" %then %put VALUE=SAS;
%else %put VALUE=other;
%mend SAS_display;
11940  %SAS_DISPLAY(SAS-A);
VALUE=SAS-A
11941  %SAS_DISPLAY(SAS_B);
VALUE=SAS_B
11942  %SAS_DISPLAY(SAS);
VALUE=SAS
11943  %SAS_DISPLAY(A+B);
VALUE=other

 

arunvaibhav2
Calcite | Level 5

You should use same format while using and passing SAS macro parameter. 

 

When your macro parameter passing value has characters, should use %str every where.

 

I have modified the code as below.

%macro SAS_display(value=);

/*1st line*/%if &value = %str(SAS-A)  %then %do; %let valueplay = %str(SAS-A); %put The value of valueplay is &valueplay; %end;
/*2nd line*/%if &value = %str(SAS_B) %then %do; %let valuedisplay = %str(SAS_B); %put The value of valuedisplay is &valuedisplay; %end; 
/*3rd line*/%if &value = SAS %then %do; %let programdisplay = SAS; %put The value of programdisplay is &programdisplay; %end;
/*4th line*/%if &value = %str(SAS%-A) %then %do; %let valueplay = %str(SAS-A); %put The value of valueplay is &valueplay; %end;
/*5th line*/%if &value = %str(SAS%_B) %then %do; %let valuedisp =%str(SAS-b); %put The value of valueplay is &valueplay; %end;

%mend SAS_display;

%SAS_display(value=SAS)
%SAS_display(value=SAS_B)

When you call what ever value in the macro, corresponding step executes and macro variables  resolves correctly.

Hope this helps.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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