User:TobyDunn/BlogEntry: 2008 April 08 22:25:16 EDT

From sasCommunity

Jump to: navigation, search

[edit] VarMatch Macro

Have you ever wanted to select variables based off of the leading or ending character(s)? When some set of leading character(s) is wanted the Colon comes in very handy.

Data Need ;
Set Have ( Keep = Var: ) ;
<Some SAS Code>  
Run ;

This Keeps all variables starting with 'Var'.


However, there is no such trick to grab only variables that end in a certain set of character(s). To that end I created the VarMatch macro which will not do both but allows for other crazy pattern you may or may not want to match variable names with.


%Macro VarMatch( DSN = , Pattern = , PRX= ) ;
/****************************************************************************/
/** Macro Name : VarMatch                                                  **/
/**                                                                        **/
/**                                                                        **/
/** Purpose    : Returns A List Of Variables From A Data Set That Matces A **/
/**              Given Pattern.                                            **/
/**                                                                        **/
/** Parameters : DSN ~ Data Set To Get Variables From.  It Can Be Either   **/
/**                    Just A Member Name Which Cause The Macro To Look    **/
/**                    In The Work Directory Or A Two Level Name.          **/
/**                                                                        **/
/**              Pattern ~ Is A Simple Pattern Where The User Gives The    **/
/**                        Known Letters in The Variables Name Seperate By **/
/**                        * For Those They Do Not.                        **/
/**                        Examples: ABC* = All Vars That Start With ABC   **/
/**                                  *ABC = All Vars That End With ABC     **/
/**                                  A*C  = All Vars That Start With A And **/
/**                                         End With C.                    **/
/**                                                                        **/
/**              PRX ~ Is A Perl Regular Expression.  This Is Designed For **/
/**                    Advanced Users Who Fully Understand RegEx.          **/
/**                                                                        **/
/** Created By: Toby Dunn                              *****************   **/
/** Created On: 03/12/2008                             * Make It Right *   **/
/**                                                    *****************   **/       
/****************************************************************************/

%Local DSID VarNum I VarName Pattern VarList Close ;

%If ( %Length(&DSN) EQ 0 ) %Then %Do ;
  %Put ;
  %Put ;
  %Put ERROR: The DSN Parameter Is Empty. ;
  %Put ERROR: Please Specify A Valid Value.  ;
  %Put ;
  %Put ;
  %Return ;
%End ;

%If ( %SysFunc( Exist( &DSN ) ) EQ 0 ) %Then %Do ;
  %Put ;
  %Put ;
  %Put ERROR: The Data Set [&DSN] Does Not Exist!!! ;
  %Put ERROR: Please Check The Value Of DSN= Parameter ;
  %Put ;
  %Put ;
  %Return ;
%End ;

%If ( %Length( &Pattern ) EQ 0 ) And 
    ( %Length( &PRX ) EQ 0 ) %Then %Do ;
  %Put ;
  %Put ;
  %Put ERROR: There Is No Pattern Or RegEx To Match. ;
  %Put ERROR: Please Specify A Valid Value For Pattern Or PRX.  ;
  %Put ;
  %Put ;
  %Return ;
%End ;


%If ( %Length( &Pattern ) GT 0 ) And 
    ( %Length( &PRX ) GT 0 ) %Then %Do ;
  %Put ;
  %Put ;
  %Put NOTE: A Value Was Given For Both Pattern And PRX. ;
  %Put NOTE: The PRX RegEx Will Be Used.  ;
  %Put ;
  %Put ;
%End ;


%Let DSID = %Sysfunc( Open( &DSN , I ) ) ;   

%If ( &DSID EQ 0 ) %Then %Do ;
  %Put ;
  %Put ;
  %Put ERROR: Data Set [&DSN] Could Not Be Opened. ;
  %Put %SysFunc( SysMSG() ) ; 
  %Put ;
  %Put ;
%End ;


%Let VarNum  = %SysFunc( AttrN( &DSID , NVars ) ) ;                                                                                                 

%If ( &VarNum EQ 0 ) %Then %Do ;
  %Put ;
  %Put ;
  %Put ERROR: Data Set [&DSN] Has No Variables. ;
  %Put ;
  %Put ;
%End ;



%If ( %Length( &PRX ) > 0 ) %Then %Do ;
  %Let Pattern = %SysFunc( PRXParse( &PRX ) ) ;
%End ;
%Else %Do ;
  %Let Pattern = %SysFunc( TranWrd( &Pattern , * , .* ) ) ;
  %Let Pattern = %SysFunc( PRXParse( /^&Pattern$/i ) ) ; 
%End ;

%Do I = 1 %To &VarNum ;
  %Let VarName = %Sysfunc( VarName( &DSID , &I ) ) ;

  %If ( %SysFunc( PRXMatch( &Pattern , &VarName ) ) > 0 ) %Then %Do ;
    %Let VarList = &VarList &VarName ; 
  %End ;
%End ;

%Let Close = %SysFunc( Close( &DSID ) ) ;


%If ( %Length( &VarList ) EQ 0 ) %Then %Do ;
  %Put ;
  %Put ;
  %Put NOTE: Could Not Find A Variable That Matched Pattern. ;
  %Put ;
  %Put ;
%End ;

&VarList 

%Mend VarMatch ;


Example:

Data Have ;
ABDC = 1 ;
ABCD = 2 ;
XYZC = 3 ;
ABFC = 4 ;
Run ;

Title "Print Out For Vars Ending With C" ;
Proc Print
 Data = Have ;
 Var %VarMatch( DSN = Have , Pattern = *C ) ;
Run ;


Output:
Print Out For Vars Ending With C                                
  Obs    ABDC    XYZC    ABFC
   1      1       3       4

Personal tools