IN operator is used in the data step to replace multiple OR conditions. However, if you try to use IN Operator in SAS Macro then you will find that the IN operator is not available.
%macro test(names); %if &names in('Alice','James','John','Judy') %then %do; %put &names; %end; %mend; %test(names=Alice);
Upon attempting to execute this macro, an error message will appear along with the macro being terminated.
ERROR: Required operator not found in expression: &names in(‘Alice’,’James’,’John’,’Judy’) ERROR: The macro TEST will stop executing.
Using the IN (#) Operator in SAS Macro
In order to use the IN operator inside a macro, there are 2 system options you need to specify.
- MINOPERATOR- Enabling this option, you can use IN (#) operator with the list of values. If the delimiter in the match list is not space, it must be specified with the
- MINDELIMITER– It is used to specify the delimiter for the list of values used in the IN operator. By default, this option is set to a single space.
Example 1: Using the IN Operator in the SAS macro
The following example uses the macro IN operator to search a character string:
options minoperator; %macro test(names); %if %eval(%upcase(&names) IN ALICE JAMES JOHN JUDY) %then %do; %put &names exists in the list; %end; %mend; %test(names=ALICE);
Any of the below statements also works fine once you enable the MINOPERATOR system options.
%if %eval(%upcase(&names) # ALICE JAMES JOHN JUDY) %then %if %upcase(&names) IN ALICE JAMES JOHN JUDY %then %if %upcase(&names) # ALICE JAMES JOHN JUDY %then
Use the MINOPERATOR system option or in the %MACRO statement if you want to use the IN (#)
To use IN or # as operators in expressions evaluated during the execution of a specific macro, use the MINOPERATOR keyword on the definition of the macro.
%macro macroname / minoperator;
Example 2: Using the IN Operator with a comma-separated list of values
The following is an example using a specified delimiter in an IN operator:
%macro test(names)/ minoperator mindelimiter=','; %if %upcase(&names) # ALICE,JAMES,JOHN,JUDY %then %do; %put &names; %end; run; %mend; %test(names=Alice);
Difference between the DATA STEP IN and MACRO IN Operator
The macro IN operator is similar to the DATA step IN operator, but there are a few differences that you need to know.
- The macro IN operator cannot search arrays.
- The colon operator (:) cannot be used as a shorthand notation to specify a range.
- The default delimiter for list elements is a blank.
- Both operands must contain a value.
%put %eval(names IN ALICE JAMES JOHN JUDY); /*Both operands are present. */
If an operand contains a null value, an error is generated.
%put %eval( IN ALICE JAMES JOHN JUDY); /*Missing first operand. */
%put %eval( names IN ); /*Missing first operand. */
Whether the first or second operand contains a null value, the same error is written to the SAS log:
ERROR: Operand missing for # operator in argument to %EVAL function.
Using NOT in with the IN operator in Macro
The IN operator compares the value of the operand on the left side of the equal sign against the list of values in the operand on the right side and returns a Boolean value.
If a match is found in the right-hand operand 1 is returned otherwise it returns 0. You can use the NOT operator to negate the return value.
Example: 3 Check if a macro value is not any of the values from the list
The following example uses the macro IN operator to search a character string.
%macro test(names)/ minoperator; %if not(%UPCASE(&names) in ALICE JAMES JOHN JUDY) %then %put &names is not in list; %else %put &names is in list; %mend test; %test(names=Ali);
Ali is not in list
So, this is the way you can use the IN Operator in a SAS Macro. You’ll notice that the syntax differs a little from Data Step IN Operator. In short, there are no brackets around the list of values, and the values are separated by spaces.
Also, the default delimiter is the space so you may need to change that using the MINDELIMITER= to specify an alternate delimiter.
You can use the hash character (#) instead of “in”, but I suggest that would just make your code hard to read.
I really hope that you must have found it useful.
Moreover, if you have any other suggestions regarding other tips or tricks to add, then please suggest them in the comment section. We’d love to include those lists in our next blog post.
Thanks for reading!
Do you have any tips to add Let us know in the comments.