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);
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 MINDELIMITER= option.
- 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 SAS macro
%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
You can also use the MINOPERATOR as below.
%macro macroname / minoperator;
Example 2: Using the IN Operator with a comma-separated list of values
%macro test(names); %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 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 defaults delimiter for list elements is a blank.
- Both the 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
%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 suggest us below the comment section. We would really take those lists in our further blog post.
Thanks for reading!
Do you have any tips to add Let us know in the comments?