How to use the IN operator in SAS macro?4 min read

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;
run;
%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

options MINOPERATOR;
%macro test(names);
%if %eval(%upcase(&names) IN ALICE JAMES JOHN JUDY) %then %do;
%put &names exists in the list;
%end;
run;
%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 %do;
%if %upcase(&names) IN ALICE JAMES JOHN JUDY %then %do;
%if %upcase(&names) # ALICE JAMES JOHN JUDY %then %do;

You can also use the MINOPERATOR as below.

%macro macroname / minoperator;

Example 2: Using the IN Operator with a comma-separated list of values

options MINOPERATOR MINDELIMITER=',';
%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. */

or

%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);

LOG results:

71 %macro test(names)/ minoperator;
72 %if not(%UPCASE(&names) in ALICE JAMES JOHN JUDY) %then %put &names is not in list;
73 %else %put &names is in list;
74 %mend test;
75 %test(names=Ali);
Ali is not in list

Key Takeaway

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's 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 delimeter.

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 suggestion regarding other tips or tricks to add then suggest us below the comment section. We would really take those list in our further blog post.

Thanks for reading!

If you liked this article, you might also want to read SSMDEL in SAS and CALL SYMPUT as well.

Do you any tips to add Let us know in the comments.

Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.

by Subhro Kar
Been in the realm with the professionals of the IT industry. I am passionate about Coding, Blogging, Web Designing and deliver creative and useful content for a wide array of audience.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap