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

/* 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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Quentin
Super User
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.
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
SAS_N00b
Fluorite | Level 6

@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!

Quentin
Super User

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!;

 

 

 

 

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
ballardw
Super User

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

SAS_N00b
Fluorite | Level 6

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

Astounding
PROC Star

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!

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!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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