Macro if not in (char list)

Frequent Contributor
Posts: 76

Macro if not in (char list)

Hi All,


I want to run something like below. 

Any simple way to run %if &var_Status. not in ('XP','XC','TP') %then %do;


Below is the error what I get-

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: "&var_LastCampaignSetStatusCode." not in Smiley Sad'XP','XC','TP')
ERROR: The macro CHECKCONTROL will stop executing.



%macro CheckControl;
/*Get the status of previous campaign load*/
proc sql;
select ID, Status
into :var_ID, :var_Status
from SomeTable
having ID = max(ID);

%if &var_Status. not in ('XP','XC','TP') %then %do;
/*Do something*/
%else %do;
/*Do something else*/
%mend CheckControl;

Super User
Posts: 5,372

Re: Macro if not in (char list)

Macro language does contain an IN operator, but it is extremely tricky.  I would recommend just spelling out the conditions in this case:


%if (&var_Status ne XP) and (&var_Status ne XC) and (&var_Status ne TP) %then %do;

Valued Guide
Posts: 950

Re: Macro if not in (char list)

[ Edited ]

How about


%if %sysfunc(findw(XP|XC|TP,&varstatus,|))=0 %then %do;


%if %sysfunc(findw(%str(XP,XC,TP),&varstatus,%str(,)))=0 %then %do;

Super User
Posts: 7,470

Re: Macro if not in (char list)

Since the macro processor only knows one data type (text), quotes are not necessary and wrong in most places. You also must not use commas to separate the values in your list.

For the use of your intended construct, see

Note the / minoperator option in the %macro statement.

So define your macro like that:

%macro checkcontrol / minoperator;


%if not(&var_Status. in XP XC TP) %then %do;


Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,727

Re: Macro if not in (char list)

This is matter of perspective really.  You can simply the problem down to:

%macro CheckControl();
%mend CheckControl;

data _null_;
  set sometable;
  by id;
  if and var_status in ('XP','XC','TP') then 
    call execute('%CheckControl;');

Assumes sorted.  In fsct, depending on the previous code the above may not be necessary at all.  Quite a lot of the conditional macro logic is just put in programs due to the process being badly thought out, or previous decisions making processing further down a program harder than they need to be.

Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation