Desktop productivity for business analysts and programmers

A character operand was found in the %EVAL function or %IF condition

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

A character operand was found in the %EVAL function or %IF condition

/* Here is the code that is having an issue*/

%macro numstats(var = ,file=, format=);
%let dsid=open(&file.,i);
%if %LENGTH(&var.) > 8 AND %VARTYPE(&dsid.,%VARNUM(&dsid.,&var.))='N' %then %do;
Proc SQL;

SQL code

quit;

end;

%mend numstats;

 

I am having the following error when running this code: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required.

 

I have played with this code extensively to try different online solutions and cannot figure out why I am still receiving this issue. Please help!

 

* Side Note: The reason I had to add this IF statement is to try to find out whether the variable was a date variable. All my date variables are Date9. so if it is Numeric and has a length greater than 8, then I want to add date formatting that I would not add to regular numeric variables. If anyone can think of an easier way to do that, then i am open to that as well, but please help me figure out this error!

 


Accepted Solutions
Solution
‎10-07-2015 04:07 PM
Super User
Posts: 5,368

Re: A character operand was found in the %EVAL function or %IF condition

As has been noted, detecting date variables properly is a separate question and that issue needs some work.  In the meantime, to address the error, try it this way:

 

%if %LENGTH(&var.) > 8 AND %sysfunc(VARTYPE(&dsid.,%sysfunc(VARNUM(&dsid.,&var.))))=N %then %do;

 

Note that the quotes around N are removed!

View solution in original post


All Replies
PROC Star
Posts: 1,293

Re: A character operand was found in the %EVAL function or %IF condition

Can you post your the code for the %VARTYPE and %VARNUM macros you call? Note that it's a good idea to CLOSE() the dataset at the end of your macro. Also note that %if %Length(&var)>8 will test whether the name if the variable is more than 8 characters. That is, if you call %NumStats(var=Fred) then &var would have a length of 4 (because Fred is four characters long). Sounds like that might not be what you want.
Occasional Contributor
Posts: 5

Re: A character operand was found in the %EVAL function or %IF condition

@Quentin

 

The %VARTYPE and %VARNUM are not macros that I am calling, but I thought were needed in the same way that I need %IF rather than just an IF statement. If this is not true, please let me know. This was a new concept to me that I was not sure about, but came across when researching solutions on the web.

 

Also, I am wanting to know whether the length of the field in general is greater than 8, rather than the specific variable name. How would I alter that? I thought it would be understandable because it is not in quotes. Please give a solution if you are aware of a fix.

 

Thank you!

PROC Star
Posts: 1,293

Re: A character operand was found in the %EVAL function or %IF condition

Ahh, well looks like @Astounding has already guessed the answer to the main part of your question.  When you say %VarType and %VarNum are not macro calls, they in fact are.  And in your log before the error message you should have seen:

WARNING: Apparent invocation of macro VARTYPE not resolved.

That is SAS's way of telling you that you tried to call a macro that doesn't exist.  And so it just leaves the macro call there, and that is causing the error for your %IF statement.  You can call these functions by nesting them in %SYSFUNC().

 

Sounds like you might be better off checking to see if the variable is formatted as DATE9, with something like:

 

%if %sysfunc(VARFMT(&dsid.,%sysfunc(VARNUM(&dsid.,&var))))=DATE9. %then %put found a date var!;

 

 

 

 

Super User
Posts: 11,134

Re: A character operand was found in the %EVAL function or %IF condition

The macro processor %IF works a little different than the data datastep IF and you may need to provide additional paratheses to get the order of evaluation correct. I think that it is processing AND %VARTYPE results (numeric and chracter) not the result of (%vartype() = 'N').

Occasional Contributor
Posts: 5

Re: A character operand was found in the %EVAL function or %IF condition

@BA

 

Initially, the AND was causing issues and I was getting a slightly different issue. I will try to dig through the differences in syntax between %IF vs. IF online. Thank you for the heads up.

Solution
‎10-07-2015 04:07 PM
Super User
Posts: 5,368

Re: A character operand was found in the %EVAL function or %IF condition

As has been noted, detecting date variables properly is a separate question and that issue needs some work.  In the meantime, to address the error, try it this way:

 

%if %LENGTH(&var.) > 8 AND %sysfunc(VARTYPE(&dsid.,%sysfunc(VARNUM(&dsid.,&var.))))=N %then %do;

 

Note that the quotes around N are removed!

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 711 views
  • 0 likes
  • 4 in conversation