Issue 27 - June 2007 - SAS UK - In The Know - Enewsletter

How to create DATA step operators that aren't in the Macro Language

You’ll find that operators in macro expressions are a subset of those in the DATA step. The macro facility does not recognise some of the operators you may have used when coding in the DATA step. This includes the following operators:

Colon (:) modifier
Special WHERE expression operators
IN operator

You can write your own macros to simulate the functionality of these operators, producing a Boolean result.

Here is an example of creating the macro version of the DATA step IN operator:

%macro in(var,list);                                                                                                                    
   %local word i found;                                                                                                                 
   %let found=0;                                                                                                                        
   %let i=1;                                                                                                                            
   %let word=%scan(&list,1);                                                                                                            
   %do %while(&word ne and not &found);                                                                                               
      %if &var eq &word %then %let found=1;                                                                                             
      %else %let found=0;                                                                                                               
      %let i=%eval(&i+1);                                                                                                               
      %let word=%scan(&list,&i);                                                                                                        
   %end;                                                                                                                                
   &found                                                                                                                               
%mend in; 

Here is how you would then call the Macro IN operator:

%macro check(parm);
   %local validlist;                                                                                                                     
   %let validlist=DALLAS SEATTLE BOSTON;                                                                                                
   %if %in(%upcase(&parm),&validlist) %then                                                                                             
      %put &parm is a valid parameter value.;                                                                                           
   %else                                                                                                                                
      %put Sorry, &parm is not a valid value.;                                                                                
%mend check; 
 
%check(Dallas)                                                                                                                          
%check(LA)

Here is a partial SAS Log:

Dallas is a valid parameter value.
Sorry, LA is not a valid value.

Click here to visit www.sas.com/uk