From: Matthew Pettis on
Y'all can ignore this... once again, more refined searching of SAS-L
AFTER doing my post yielded results:

=20

http://www.listserv.uga.edu/cgi-bin/wa?A2=3Dind0803B&L=3Dsas-l&P=3DR14170=


=20

Sorry to bother,

matt

=20

From: Pettis, Matthew (Legal)=20
Sent: Tuesday, January 12, 2010 2:20 PM
To: SAS-L(a)LISTSERV.UGA.EDU
Subject: Using a Colon (:) wildcard at beginning of variable lists

=20

Hi,

=20

I know that you can use a colon wildcard with a prefix in a drop
statement (among others) to get a list of things with a common prefix.
For example, if I have a dataset work with the columns:

=20

X

Y

ID_A

ID_B

Z

=20

And issue:

=20

Data work;

Set work;

Drop ID_:;

Run;

=20

Then only the vars X, Y, and Z will be left in work, and ID_A and ID_B
will be dropped.

=20

I, however, have the case where the variable names are like:

=20

X

Y

A_ID

B_ID

Z

=20

But the natural construct that follows does not work:

=20

Data work;

Set work;

Drop :_ID;

Run;

=20

Is there some idiom/syntax that allows me to wildcard based on a common
suffix, rather than a common prefix with the colon wildcard?

=20

Thanks,
Matt
From: Toby Dunn on
Matt,

I just use a macro I developed a while back:

Scaled down version minus all the ussual header info and error checking.
Lets hope I didn't cut anything out that was important.



%macro varmatch( DSN = , Pattern = , PRX= ) ;
%Local DSID VarNum I VarName Pattern VarList Close ;

%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 New ;
Set Have ( Drop = %VarMatch( Data = Have , Pattern = *_ID ) ) ;
Run ;









On Tue, 12 Jan 2010 14:19:45 -0600, Matthew Pettis
<matt.pettis(a)THOMSONREUTERS.COM> wrote:

>Hi,
>
>
>
>I know that you can use a colon wildcard with a prefix in a drop
>statement (among others) to get a list of things with a common prefix.
>For example, if I have a dataset work with the columns:
>
>
>
>X
>
>Y
>
>ID_A
>
>ID_B
>
>Z
>
>
>
>And issue:
>
>
>
>Data work;
>
> Set work;
>
> Drop ID_:;
>
>Run;
>
>
>
>Then only the vars X, Y, and Z will be left in work, and ID_A and ID_B
>will be dropped.
>
>
>
>I, however, have the case where the variable names are like:
>
>
>
>X
>
>Y
>
>A_ID
>
>B_ID
>
>Z
>
>
>
>But the natural construct that follows does not work:
>
>
>
>Data work;
>
> Set work;
>
> Drop :_ID;
>
>Run;
>
>
>
>Is there some idiom/syntax that allows me to wildcard based on a common
>suffix, rather than a common prefix with the colon wildcard?
>
>
>
>Thanks,
>Matt