DATA Step, Macro, Functions and more

Comparing a macro variable with multiple values

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Comparing a macro variable with multiple values

Hi guys,

 

I am new to SAS and got stuck with something :

 

When I try to do this

 

%LET SITE=CBH;

 

%if &site. in AMH, AUH, BSB, CBH, INM, MNL, SGH, VNM %then %do;

 

 

%END;

 

and execute the code, in the log I observe that the condition is failing,

 

SYMBOLGEN: Macro variable SITE resolves to CBH

MLOGIC(ALN): %IF condition &site. in AMH, AUH, BSB, CBH, INM, MNL, SGH, VNM is FALSE

 

When I did the same thing in a different place, like

%if ^(&site in DAK, NZM, SEL) %then %do;  

 

%end;

 

then it worked fine. Can you help me with this issue. Thanks in advance


Accepted Solutions
Solution
‎07-24-2017 11:08 PM
Super User
Posts: 5,503

Re: Comparing a macro variable with multiple values

Posted in reply to nithin_kp

Macro language is quite fussy about using the IN operator.  Because there are no quotes around values, you have to tell macro language what delimiter to use, and you cannot add spaces between values (unless a space is your delimiter of course).  For example:

 

 

%LET SITE=CBH;

options mindelimiter=',' ;

 %if &site. in AMH,AUH,BSB,CBH,INM,MNL,SGH,VNM %then %do;

 %END;

View solution in original post


All Replies
Solution
‎07-24-2017 11:08 PM
Super User
Posts: 5,503

Re: Comparing a macro variable with multiple values

Posted in reply to nithin_kp

Macro language is quite fussy about using the IN operator.  Because there are no quotes around values, you have to tell macro language what delimiter to use, and you cannot add spaces between values (unless a space is your delimiter of course).  For example:

 

 

%LET SITE=CBH;

options mindelimiter=',' ;

 %if &site. in AMH,AUH,BSB,CBH,INM,MNL,SGH,VNM %then %do;

 %END;

Super User
Super User
Posts: 7,041

Re: Comparing a macro variable with multiple values

Posted in reply to nithin_kp

Did you set the options that allow the macro processor to treat the text IN as an operator instead just treating it like any other text string?

MINDELIMITER=     Specifies the character delimiter for the macro IN operator.
NOMINOPERATOR     Disables IN logical operators in expressions.

Or if your code is inside a macro did you set the option on the %MACRO statement to enable the IN operator?

MINDELIMITER='single character';
specifies a value that will override the value of the MINDELIMITER= global option. The value 
must be a single character enclosed in single quotation marks and can appear only once 
in a %MACRO statement.

Restriction	The following characters cannot be used as a delimiter
%  &  '  "  (  )  ;  

MINOPERATOR | NOMINOPERATOR
specifies that the macro processor recognizes and evaluates the mnemonic IN and the 
special character # as logical operators when evaluating arithmetic or logical 
expressions during the execution of the macro. The setting of this argument overrides 
the setting of the NOMINOPERATOR global system option.

The NOMINOPERATOR argument specifies that the macro processor does not recognize the 
mnemonic IN and the special character # as logical operators when evaluating arithmetic 
or logical expressions during the execution of the macro. The setting of this argument 
overrides the setting of the MINOPERATOR global system option.

Also I think that the () are required.

So if MINOPERaTOR is set and MINDELIMITER is set to ',' then this should work.

%if &site. in (AMH,AUH,BSB,CBH,INM,MNL,SGH,VNM) %then %do;
New Contributor
Posts: 2

Re: Comparing a macro variable with multiple values

Thanks both of you; Included both the options, now it's working fine.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 216 views
  • 1 like
  • 3 in conversation