Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

A character operand was found in the %EVAL function or %IF condition where a numeric operand is required

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

A character operand was found in the %EVAL function or %IF condition where a numeric operand is required

I have this code inside a macro:

%if &macro_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: &macro_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.


Accepted Solutions
Solution
‎12-31-2014 04:24 PM
Super User
Super User
Posts: 6,500

Re: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required

Since the macro variable doesn't exist (see the first warning message) it is attempting to evaluate the expression &macro_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 "&macro_var"="" %then ...

View solution in original post


All Replies
PROC Star
Posts: 7,363

Re: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required

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 &macro_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( )

Frequent Contributor
Posts: 82

Re: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required

Thank you. You can replicate the error like this:

%macro test();

  %if &macro_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();

Solution
‎12-31-2014 04:24 PM
Super User
Super User
Posts: 6,500

Re: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required

Since the macro variable doesn't exist (see the first warning message) it is attempting to evaluate the expression &macro_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 "&macro_var"="" %then ...

PROC Star
Posts: 7,363

Re: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required

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 &macro_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();

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 13546 views
  • 3 likes
  • 3 in conversation