DATA Step, Macro, Functions and more

vartype in macro: ERROR: Required operator not found in expression:

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 127
Accepted Solution

vartype in macro: ERROR: Required operator not found in expression:

I am runing the following macro

 

%macro macro_outliers (var=);

(...)

%if (vartype(base,&VAR.)='N') %then %do;
(...)
%end;
%else %do; %end;
%mend macro_outliers;

 

and I get the following error message:

ERROR: Required operator not found in expression: (vartype(base,&VAR.)='N')
ERROR: The macro MACRO_OUTLIERS will stop executing.

 

since the variable is numeric, the macro should not stop. what is wrong in the check on the variable type?


Accepted Solutions
Solution
‎06-30-2016 07:01 AM
Contributor
Posts: 39

Re: vartype in macro: ERROR: Required operator not found in expression:

As Reeza & Kurt said, you need to use %sysfunc

 

See example 1 in http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000148443.htm

 

%if (%sysfunc(vartype(&dsid,&i)) = N) %then

 

 

If you want to change it to use the  %DATATYP autocall function, need to use the MAUTOSOURCE system option

 

View solution in original post


All Replies
Super User
Posts: 17,840

Re: vartype in macro: ERROR: Required operator not found in expression:

You should post more of the code. 

 

Usually you need to wrap functions in %sysfunc() within macros  

Super User
Posts: 6,946

Re: vartype in macro: ERROR: Required operator not found in expression:

In a macro %if conditition, you can only use

- literals

- macro functions

- macro variables

To use a data step function, you need to wrap it into a %sysfunc (which is a macro function) call.

But in your case, I guess you are mistaken about what you can do.

The vartype function needs a numeric file handle reference to a dataset (coming from the open function) as its first parameter, which will only be present in the context of a running data step (keep in mind that macro processing always occurs in its own context).

I suggest that you apply the function in a data _null_ step inside your macro and use call symput to save the result in a macro variable, which can then be used in further macro logic.

 

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 127

Re: vartype in macro: ERROR: Required operator not found in expression:

Dear experts,

 

I am trying to do something easy that in another language (e.g. R, Python) would be done typing maximum ten times on a keyboard, just a really short line: check the format of a variable in a macro.

I need a solution to do directly and easily what I need, thanks for your effort inhelping but please answer with a concrete, straighforward and  easy solutions (if you have it) proportional to the banality of the problem I am posting.

 

I did the following change but there is still something not working.

 

%if
/* (vartype(base,&VAR.)='N') */
%datatyp(&VAR.)=NUMERIC
%then %do;

Solution
‎06-30-2016 07:01 AM
Contributor
Posts: 39

Re: vartype in macro: ERROR: Required operator not found in expression:

As Reeza & Kurt said, you need to use %sysfunc

 

See example 1 in http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000148443.htm

 

%if (%sysfunc(vartype(&dsid,&i)) = N) %then

 

 

If you want to change it to use the  %DATATYP autocall function, need to use the MAUTOSOURCE system option

 

Frequent Contributor
Posts: 127

Re: vartype in macro: ERROR: Required operator not found in expression:

Thanks!

Super User
Posts: 6,946

Re: vartype in macro: ERROR: Required operator not found in expression:

The %datatyp autocall macro determines the datatype of a typical macro text expression (always keep in mind that the macro language is first and foremost a text processor!)

Therefore it is of utmost importance what the contents of &var are.

If &var contains a variable name, then that name will be the argument of %datatyp, which then will always return CHAR (SAS names are never pure numbers). If &var contains the name of another macro variable, using &&&var might do the trick.

If you want to determine the type of a dataset variable for further use in macro processing, use a data _null_ step.

Either OPEN the dataset and use the VARTYPE function, or extract the information from SASHELP.VCOLUMN, and then use call symput.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 1148 views
  • 1 like
  • 4 in conversation