Using the IN Operator with the Macro Language in SAS 9.2
In data step code it is common practice to use an IN operator in place of lots of OR statements, this allows you to write code that is concise and less repetitive. In addition the code also runs more efficiently as SAS no longer needs to make multiple evaluations.
data subset; set sashelp.class; *written in full; if name='Alice' or name='James' or name='John' or name='Judy'; *abbreviated using the IN operator; if name in('Alice','James','John','Judy'); run;
proc print data=subset; run;
However if you try to apply the same technique within the macro language then you find that the IN operator is not available:
*macro IN operator; %macro inoperator(proc,ds); %if %upcase(&proc) in (MEANS,PRINT) %then %do; proc &proc data=&ds; run; %end; %mend inoperator; %inoperator(print,sashelp.class);
Attempting to execute this macro will result in an error message as well as the macro being terminated early.
19 %inoperator(print,sashelp.class); ERROR: Required operator not found in expression: %upcase(&proc) in (MEANS,PRINT) ERROR: The macro INOPERATOR will stop executing.
Version 9.2 of SAS now allows you to use an IN operator within the macro language.
In order to use the IN operator, two SAS options need to be considered:
- MINOPERATOR: This is the most important of the two options as, when set, this allows the IN operator to be used.
- MINDELIMITER=: Defines a single character to be used as a delimiter for the list of values used in the IN operator. By default this option is set to a single space.
*macro IN operator; options minoperator mindelimiter=','; %macro inoperator(proc,ds); %if %upcase(&proc) in (MEANS,PRINT) %then %do; proc &proc data=&ds; run; %end; %mend inoperator; %inoperator(print,sashelp.class);
In the above example the MINDELIMITER option has been set to a comma, this allows the values to be separated by commas as they are in the data step version of the IN operator.
Invoking the macro now gives the desired result, as the value of &proc is now evaluated against the list of values.
Consultant Ann White also demonstrates techniques for using the macro IN operator in SAS 9.2.: