Go Back   Rhinocerus > Newsgroup > Newsgroup comp.soft-sys.sas

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-29-2005, 10:42 AM
Guido T
Guest
 
Posts: n/a
Default Re: preserve case using CALL PRXCHANGE

Hi,

I can't think of a really nice, elegant way to do this using RX, PRX or an
base SAS.

So here's an old brute force approach (hidden in a macro).

%MACRO rependst(STRING=, SFROM=, STO=);
%LET SFROM=%UPCASE(&SFROM.);
%LET STO=%UPCASE(&STO.);
__found = index(trim(upcase(&STRING.))||'~~~', "%TRIM(&SFROM.)~~~");
if __found then do;
do __i=__found to length(&STRING.);
if 'a' le substr(&STRING.,__i,1) le 'z'
then substr(&STRING.,__i,1) =
lowcase(substr("&STO.",__i-__found+1,1) );
else substr(&STRING.,__i,1) =
substr("&STO.",__i-__found+1,1);
end;
end;
drop __i __found;
%MEND;

data test;
length s $80;
input s $80.;
put 'Before : ' s=;
%REPENDST(STRING=s, SFROM=ABCDEF, STO=UVWXYZ);
put 'After : ' s=;
datalines;
This is the old string, which ends on abcDEF
THIS IS THE OLD STRING, WHICH ENDS ON ABCDEF
THIS IS THE OLD STRING, WHICH ENDS ON AbCdEf
Nothing should be replaced in this string ABCDE
;
run;

In the log :-
Before : s=This is the old string, which ends on abcDEF
After : s=This is the old string, which ends on uvwXYZ
Before : s=THIS IS THE OLD STRING, WHICH ENDS ON ABCDEF
After : s=THIS IS THE OLD STRING, WHICH ENDS ON UVWXYZ
Before : s=THIS IS THE OLD STRING, WHICH ENDS ON AbCdEf
After : s=THIS IS THE OLD STRING, WHICH ENDS ON UvWxYz
Before : s=Nothing should be replaced in this string ABCDE
After : s=Nothing should be replaced in this string ABCDE

There must be a simpler way.

Regards
++ Guido


>From: Venky Chakravarthy <swovcc@HOTMAIL.COM>
>Reply-To: Venky Chakravarthy <swovcc@HOTMAIL.COM>
>To: SAS-L@LISTSERV.UGA.EDU
>Subject: Re: preserve case using CALL PRXCHANGE
>Date: Thu, 28 Jul 2005 19:35:42 -0400
>
>This is the second instance in recent times that an elegant use of the
>TRANSLATE function has been seen on the list. Borrowing from this, my
>earlier solution can be reduced to a one liner as:
>
>data _null_;
> length old new $200;
> infile datalines truncover;
> input old $200.;
> new = old ;
> substr(new,length(new)-2)=
> translate(substr(new,length(new)-2),"xyzXYZ","abcABC");
> put old=/new=/;
> datalines;
>This is the old string, which ends on abc
>THIS IS THE OLD STRING, WHICH ENDS ON ABC
>Nothing should be replaced in this string
>;
>run;
>
>On Thu, 28 Jul 2005 19:11:19 -0400, Gerhard Hellriegel <ghellrieg@T-
>ONLINE.DE> wrote:
>
> >perhaps you could do it with SAS?
> >Something like (untested):
> >
> >if upcase(trim(reverse(old)))=:"CBA" then do;
> > new=trim(reverse(old));
> > substr(new,1,3)=translate(substr(new,1,3),"xyzXYZ" ,"abcABC");
> > new=trim(reverse(new));
> >end;
> >
> >
> >On Thu, 28 Jul 2005 14:58:41 -0700, Philip Primak <pprimak@GMAIL.COM>

>wrote:
> >
> >>Hi
> >>
> >>I need to do the following replace in a character string: if the
> >>string ends on "abc" I have to change this "abc" to "xyz", or do no
> >>replace otherwise.
> >>
> >>To accomplish this I am using pearl regular expressions with CALL
> >>PRXCHANGE. Here is my code:
> >>
> >>data _null_;
> >> length old new $200;
> >> infile datalines truncover;
> >> input old $200.;
> >>
> >> if _n_=1 then re=prxparse("s/abc *$/xyz/i");
> >> retain re;
> >> call prxchange(re,-1,old,new);
> >>
> >> put old=/new=/;
> >> datalines;
> >>This is the old string, which ends on abc
> >>THIS IS THE OLD STRING, WHICH ENDS ON ABC
> >>Nothing should be replaced in this string
> >>;
> >>run;
> >>
> >>The problem is that it changes "abc" to "xyz" regardless of the
> >>original case of "abc". So I am getting as a result:
> >>
> >>old=This is the old string, which ends on abc
> >>new=This is the old string, which ends on xyz
> >>
> >>old=THIS IS THE OLD STRING, WHICH ENDS ON ABC
> >>new=THIS IS THE OLD STRING, WHICH ENDS ON xyz
> >>
> >>old=Nothing should be replaced in this string
> >>new=Nothing should be replaced in this string
> >>
> >>while I need to get:
> >>
> >>old=This is the old string, which ends on abc
> >>new=This is the old string, which ends on xyz
> >>
> >>old=THIS IS THE OLD STRING, WHICH ENDS ON ABC
> >>new=THIS IS THE OLD STRING, WHICH ENDS ON XYZ
> >>
> >>old=Nothing should be replaced in this string
> >>new=Nothing should be replaced in this string
> >>
> >>Does anybody know how to preserve the case and get the result I am
> >>looking for?
> >>
> >>Thank you for your help.
> >>
> >>Philip Primak
> >>Genzyme Corporation

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: OT: Chance to Make SAS-L History: Did You Know That... Craig Stroup Newsgroup comp.soft-sys.sas 0 08-14-2007 08:05 PM
proc report sas to excel: break not working Bob Sayre Newsgroup comp.soft-sys.sas 1 06-29-2006 04:50 PM
Re: Read/Write Microsoft Word document William W. Viergever Newsgroup comp.soft-sys.sas 0 05-31-2006 08:46 PM
Re: Read/Write Microsoft Word document Joe Whitehurst Newsgroup comp.soft-sys.sas 0 05-31-2006 08:35 PM
Re: preserve case using CALL PRXCHANGE Chang Chung Newsgroup comp.soft-sys.sas 0 07-28-2005 11:35 PM



All times are GMT. The time now is 08:24 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.