Date: Fri, 10 Aug 2007 09:37:12 -0400
Reply-To: sas_andy@BOOSECAT.COM
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Andy Andrews <sas_andy@BOOSECAT.COM>
Subject: Re: Removing words from a string
Content-Type: text/plain; charset=UTF-8
Thanks all!!
Peetie, I'm currently doing it almost exactly how you typed out. That makes me feel good in that someone else would do it that way! :)
I've just cut-and-pasted the other code into the program and I'm going to modify to see what I can get to work. Thanks again to everyone!!
2-2 for Thursday...hopefully I can make it through Friday w/o any questions!!
AA
On Aug 9, 11:22 pm, sas_a...@BOOSECAT.COM (Andy Andrews) wrote:
> Hi all...you helped out so much earlier I thought I'd throw another bone in!
I have 2 macro variables - good and bad. I need to remove all instances of the
words in bad from good.
>
> For example:
> %let good=blue white red yellow orange purple black;
> %let bad=yellow purple;
>
> I need to remove "yellow" and "purple" from the macro variable good. I'm
currently breaking apart the variables word by word, then merging them back in
where it's in good and not in bad. This works great, however the code is a
little long and ugly. It doesn't matter how the data ends back up. It can be
a single-variable dataset (currently what I have), another macro variable,
etc.
>
> Thanks! I hope this makes sense to someone!
Andy
a problem for the classic recursion solution
assuming you have &good and &bad as macro variables in the current
environment
%let bad_i = 1 ;
%macro done( from ) ;
%if "%scan( &bad, &bad_i ) " ne " " %then %do;
%done(
%sysfunc( tranwrd( &from, %scan(&bad, &bad_i), %str( ) ) )
)
%let bad_i = %eval( 1+&bad_i );
%end ;
%mend done ;
unless you have too many bad words, then this (untested) recursive
routine might work.
I'm not sure of just how deep you can recurse in a macro.
It loops over the bad list, each time using tranwrd() to remove the
latest word from the list.
It takes the result as the parameter for the next level call.
Calling stops when the &bad list is exhausted.
It probably could use refinement to avoid substring replacement ( like
removing NAME from SURNAME ), but I expect you would know how to do
that.
Good Luck
PeterC