SAS Support
Self Service Support
Assisted Support
Premium Support
Enterprise Support
Logging a Call
Service Levels
Hints and Tips
 

HOW TO CREATE DATA STEP OPERATORS THAT AREN'T IN THE MACRO LANGUAGE

SAS modules addressed: Datastep/SQL, Misc

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.


Did you find this page useful?

If you have any comments or questions, feel free to contact us.



0845 402 9907