BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
eagles_dare13
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
art297
Opal | Level 21

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

eagles_dare13
Obsidian | Level 7

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

Tom
Super User Tom
Super User

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 ...

art297
Opal | Level 21

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

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 choose a machine learning algorithm

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.

Discussion stats
  • 4 replies
  • 83316 views
  • 6 likes
  • 3 in conversation