11-15-2017 04:48 PM - edited 11-15-2017 04:49 PM
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.
%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;
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.
11-15-2017 05:22 PM
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!
11-15-2017 06:04 PM
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.
11-15-2017 07:11 PM
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.
11-15-2017 08:36 PM
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
11-15-2017 09:21 PM
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.