/* 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!
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!
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!
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!;
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').
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.
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!
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.