User:TobyDunn/BlogEntry: 2008 April 07 22:27:06 EDT

From sasCommunity

Jump to: navigation, search

[edit] Macro In Function

In the Data Step world there is the In function. Up until recently there was for this ever so helpful little function. The SAS tried in version 9 to create one at the users request #In. However, since the # was a free character for so long that many programmers used this for all sorts of things like a separator for Macro list. Not too suprisingly it made many older programs bomb and well that breaks a SAS golden rule of always making code backwards compatible. So they disabled this feature, but Tech support was still getting calls wanting such a feature. Tech Support has this for there code:

%macro in()/parmbuff; 
%local i; 
%let parms=%qsubstr(&syspbuff,2,%length(&syspbuff)-2); 
%let var=%scan(&parms,1,%str(,)); 
%let numparms=%eval(%length(&parms)- 
              %length(%sysfunc(compress(&parms,%str(,))))); 
%let infunc=&var eq %scan(&parms,2,%str(,)); 

%do i= 3 %to (&numparms + 1); 
  %let thisparm=%scan(&parms,&i,%str(,)); 
  %let infunc=&infunc or &var eq &thisparm; 
%end; 
(&infunc) 
%mend; 

All I can say is Yuck!!!! who walked in and spewed that hunk of crap!!!!

So I wrote my own which is simplier and actially has some error handling.

%Macro IsIn( LookFor= , List= , Case=I ) ;
/***************************************************/
/** Name       : IsIn                             **/
/**                                               **/
/** Parameters : LookFor ~ Text To Search For In  **/
/**                        Target String (List).  **/
/**                                               **/
/**              List    ~ Target Sting To Search **/
/**                                               **/
/**              Case    ~ Determines Whether The **/
/**                        Search Is Case         **/
/**                        Sensitive              **/
/**                        I = Case Insensitive   **/
/**                        Blank = Case Sensitive **/
/**                                               **/
/** Purpose    : The Macro Version Of The In      **/
/**              Function. Returns 1 If It Can    **/
/**              Find The Text And 0 If It Cant   **/
/***************************************************/
%Local Pattern ;


%If ( %Length( &LookFor ) = 0 ) %Then %Do ;
  %Put ;
  %Put ERROR: Please Specify A Valid Value For The LookFor Parameter!!! ;
  %Put ;
  %Return ;
%End ;

%If ( %Length( &List ) = 0 ) %Then %Do ;
  %Put ;
  %Put ERROR: Please Specify A Valid List Of Values For The List Parameter!!! ;
  %Put ;
  %Return ;
%End ;

%If ( ( %Length( &Case ) Ne 0 ) And ( &Case NE I ) And
      ( &Case NE i ) ) %Then %Do ;
   %Let Case = I ;
   %Put ;
   %Put WARNING: The Value For Parameter Case Is Not Valid. ;
   %Put WARNING: A Value Of I Will Be Used Instead. ;
   %Put ;
%End ;

%Let Pattern = %SysFunc( PRXParse( /(?=&LookFor)/&Case ) ) ;

%Eval( %SysFunc( PRXMatch( &Pattern , &List ) ) > 0 ) ;

%Mend IsIn ;

Personal tools