LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (March 2008, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Wed, 19 Mar 2008 04:09:31 +0000
Reply-To:   toby dunn <tobydunn@HOTMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   toby dunn <tobydunn@HOTMAIL.COM>
Subject:   Re: wild card match for variable name
Comments:   To: "Richard A. DeVenezia" <rdevenezia@wildblue.net>
In-Reply-To:   <006c01c88924$1b20edf0$6401a8c0@extreme>
Content-Type:   text/plain; charset="iso-8859-1"

I presented this as an extra give me for attending my presentation at SGF this year:

%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 ;

Toby Dunn

"Don't bail. The best gold is at the bottom of barrels of crap." Randy Pausch

"Be prepared. Luck is where preparation meets opportunity." Randy Pausch

> Date: Tue, 18 Mar 2008 13:15:35 -0500 > From: rdevenezia@WILDBLUE.NET > Subject: Re: wild card match for variable name > To: SAS-L@LISTSERV.UGA.EDU > > sas_9264 wrote: >> Hi, I know sas using ':' do variable matching, but seems it only can >> do right side match, such as app: or xx_:. Now my question is how can >> I do left side match, such as all the variable names end with _grp ? > > There is no shortcut naming syntax for selecting variables by name suffix or > arbitrary regular expression. > > Maro can be used to select variables based on your on criteria. > The processing in the macro can be made more smart if you are dealing with > numeric suffixes. This might occur if you are dealing with a slice across > several arrays (in other words, want the A12 B12 C12 ... Z12 variables) > > ----------------------------- > data foo; > length a1 b1 c1 d1 e11 d21 t_grp u_grp v_grp w_grp x_grp y_grp z_grp 8; > stop; > run; > > %macro select_vars(data=, suffix=); > > %local ds i varname L; > %let ds = %sysfunc(open(&data)); > %if &ds %then %do; > %let L = %length(&suffix); > %do i = 1 %to %sysfunc(attrn(&ds,NVARS)); > %let varname=%sysfunc(varname(&ds,&i)); > %if &L = 0 %then %do; > &varname. > %end; > %else > %if %length(&varname)>= %length(&suffix) %then %do; > %if %substr(&varname,%length(&varname)-&L+1) = &suffix %then %do; > &varname. > %end; > %end; > %end; > %let ds = %sysfunc(close(&ds)); > %end; > %else %do; > %put WARNING: Problem in select_vars, data=; > %end; > %mend; > > %put %select_vars(data=foo, suffix=); > %put %select_vars(data=foo, suffix=1); > %put %select_vars(data=foo, suffix=_grp); > ----------------------------- > > -- > Richard A. DeVenezia

_________________________________________________________________ Helping your favorite cause is as easy as instant messaging. You IM, we give. http://im.live.com/Messenger/IM/Home/?source=text_hotmail_join


Back to: Top of message | Previous page | Main SAS-L page