Search This Blog

Loading...

Sunday, August 16, 2009

Macro IN Operator

Have you ever come across a situation where you have to write a macro program where a macro variable has more than one value? Writing a macro program in this case involves so many different conditions and to connect each condition you generally use OR operator as below…

%macro test;
%if &dsn=ae or %if &dsn=ds or %if &dsn=co or %if &dsn=cm %then %do; Some---SAS—Statements;
%end;
%test;

You can use a simple IN operator inside the datastep and make the code very simple… like as follows…

data dsn;
set old;
if &dsn in ("ae","ds","co","cm") then do;
Some---SAS—Statements;
end;
run;

Can we use the same IN operator inside the macro…

If you are using any SAS version prior to 9.2 … the answer is.. You cannot use IN operator inside the macro.

But…..

In SAS 9.1.3 version:
You can use character # (new binary comparison operator) as an alternate operator to mnemonic IN operator.

Using # operator in the above code:

%macro test;
%if &dsn # ae ds co cm %then %do;
Some---SAS—Statements;
%end;
%test;

Even if you use it, SAS will give you an ERROR message saying the Operator is not recognized.

In SAS 9.2 version:
You can directly use the IN operator inside your macro code as like in simple datastep in SAS 9.2.

Using IN operator in the above code:

%macro test;
%if &dsn in ae ds co cm %then %do;
Some---SAS—Statements;
%end;
%test;

Note:
No need of % sign in front of IN operator.
In SAS 9.2 both IN and # work if you use the system option minoperator inside your macro call.

In SAS Version 9.2:

%macro test/minoperator;
%if &dsn # ae ds co cm %then %do;
Some---SAS—Statements;
%end;
%test;

%macro test;
%if &dsn in ae ds co cm %then %do;
Some---SAS—Statements;
%end;
%test;


Both works fine…..

MINOPERATOR option tells SAS to recognize the word 'IN' or special symbol # by the SAS macro facility as an infix operator when evaluating logical or integer expressions.


Here is another way of writing the macro code with delimiters.

Use MINDELIMITER option to change the default delimiter from space to any other, in this case it is comma (,).

options mindelimiter;

%macro test/mindelimiter=',';
%if &dsn in ae,ds,co,cm %then %do;
Some---SAS—Statements;
%end;
%test;

3 comments:

Post a Comment

ShareThis