Use the IN operator in macro

The IN operator is one convenient checking tool in SAS which can be used in place of lots of OR statements. And it’s quite common in data step.

data test;
  if 3 in (1, 2, 3, 4, 5, 6) then
    put "Value found within list.";
  else put "Value not in list.";
run;

Sometimes, you will come across a situation where you have to write a macro program where a macro variable has more than one value. You may think to use multiple OR operators as below…

* Prior to SAS9.2, the following syntax was used;
%macro test(value); 
  %if &value=1 or &value=2 or &value=3 or &value=4 or
      &value=5 or &value=6 %then %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

Starting SAS 9.2, there is an IN operator for the macro language. The IN operator can now be used on the %IF statement when the MINOPERATOR option is set on the %MACRO statement or as a SAS system option.

%macro test(value)/minoperator;
  %if &value in 1 2 3 4 5 6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

Or you can use character # (new binary comparison operator) as an alternate operator to mnemonic IN operator.

%macro test(value)/minoperator;
  %if &value # 1 2 3 4 5 6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

They both work fine. MINOPERATOR option tells SAS to recognize the word ‘IN’ or # by the SAS macro facility as a logical operator in expressions.

And there is another way of writing code which is more close to the way we use IN operator in data step. By default, a space is used as the delimiter for the operator, but can be changed by using the MINDELIMITER= %MACRO statement option.

options minoperator;

%macro test(value)/mindelimiter=',';
  %if &value in 1,2,3,4,5,6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

 

Reference: 

http://support.sas.com/kb/35/591.html

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a003092012.htm