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?
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
You should post more of the code.
Usually you need to wrap functions in %sysfunc() within macros
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.
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;
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
Thanks!
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.
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.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.