I have this code inside a macro:
%if ¯o_var =%str() %then
%do;
%put ERROR: The macro_var is not set.;
%return;
%end;
I am getting error:
WARNING: Apparent symbolic reference macro_var not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: ¯o_var=
The WARNING is ok..because the macro_var has not been set. But Why is it throwing an error? Why is it looking for a numeric operand? How to resolve this? Basically I am trying to check if a macro variable is null or has been set with a proper value.
Since the macro variable doesn't exist (see the first warning message) it is attempting to evaluate the expression ¯o_var=. It is interpreting the & as the AND operator and so generates the error since you cannot use AND with a text string (macro_var). For example if you wrote %put %eval( 1 & 2) it would not generate an error message since the arguments for the & operator are both numeric.
You could add %IF %SYMEXIST(macro_var) %THEN to make sure the variable is defined.
You could add some quotes so that the implied %EVAL() in the %IF statement knows that you are comparing strings. %if "¯o_var"="" %then ...
You have to show more of your code (e.g., the entire macro and how you call it). Your code appears to work fine if used as below:
%macro test(macro_var);
%if ¯o_var =%str() %then
%do;
%put ERROR: The macro_var is not set.;
%return;
%end;
%else %do;
%put macro_var was set.;
%return;
%end;
%mend test;
%test(PD_OVERRIDE_FILENAME)
%test( )
Thank you. You can replicate the error like this:
%macro test();
%if ¯o_var =%str() %then
%do;
%put ERROR: The macro_var is not set.;
%return;
%end;
%else %do;
%put macro_var was set.;
%return;
%end;
%mend test;
%test();
Since the macro variable doesn't exist (see the first warning message) it is attempting to evaluate the expression ¯o_var=. It is interpreting the & as the AND operator and so generates the error since you cannot use AND with a text string (macro_var). For example if you wrote %put %eval( 1 & 2) it would not generate an error message since the arguments for the & operator are both numeric.
You could add %IF %SYMEXIST(macro_var) %THEN to make sure the variable is defined.
You could add some quotes so that the implied %EVAL() in the %IF statement knows that you are comparing strings. %if "¯o_var"="" %then ...
My suggestion is the same as Tom's. You actually have 3 conditions to test. E.g.:
%macro test();
%if %symexist(macro_var) %then %do;
%if ¯o_var =%str() %then %do;
%put ERROR: The macro_var is set but null.;
%return;
%end;
%else %do;
%put macro_var was set.;
%return;
%end;
%end;
%else %do;
%put ERROR: The macro_var is not set.;
%return;
%end;
%mend test;
%test();
%let macro_var=PD_OVERRIDE_FILENAME;
%test();
%let macro_var=;
%test();
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.
Find more tutorials on the SAS Users YouTube channel.