- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi ,
I have a word PROGRAM that can be in differnt cases (lowcase,upcase,mixed) in each observation like Program , PROGRAM , program,PRogRam
.In what ever the case it may be or at whatever position of the word i have to change this into Programming.Since tranwrd function is case sensitive , unable to convert this into required output.Is there any way that can be done wihtout impacting other strings or characters in a text.
Text Output
ex: This is my program This is my Programming
PROGRAM is mine Programming is mine.
Change PROgram Change Programming.
Can anyone clarify this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Couldn't you get what you want by upcasing text when you use tranwrd? e.g.:
data have;
informat text $50.;
input text &;
cards;
This is my program
PROGRAM is mine
Change PROgram
ThisPrOGrAMworks
This program is my program
;
data want;
set have;
text=tranwrd(upcase(text),"PROGRAM","programming");
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi Art ,
i had already tried by this method . By using upcase/lowcase it will even convert the other strings in the text into UPCASE/LOWCASE. the other strings in the text should not be altered they should be in the same case as they are.
From the below output you can see the other strings are coverted into UPCASE.
THIS IS MY programming
programming IS MINE
CHANGE programming
THIS programming WORKS
THIS programming IS MY programming
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Then how about:
data have;
informat text $50.;
input text &;
cards;
This is my program
PROGRAM is mine
Change PROgram
ThisPrOGrAMworks
This program is my program
;
data want;
set have;
do while(index(upcase(text),"PROGRAM"));
substr(text,index(upcase(text),"PROGRAM"),7)="progra~";
end;
text=tranwrd(text,"progra~","programming");
run;
proc print data=want;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
hi ... using Art's data ...
data want;
set have;
do while (find(upcase(text),"PROGRAM"));
substr(text,find(upcase(text),"PROGRAM"),7) = 'X*X*X*X';
end;
text = tranwrd(text,"X*X*X*X","Programming");
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can include FIND function modifier "i" which should allow you to remove the UPCASE funtion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
DN: You beat me to it. I don't see any other advantage to using find over index. And, Mike, he wanted lower case programming:
data want;
set have;
do while (find(text,"program",'i'));
substr(text,find(text,"program",'i'),7) = 'X*X*X*X';
end;
text = tranwrd(text,"X*X*X*X","programming");
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Arthur Tabachneck wrote:
I don't see any other advantage to using find over index.
In this example I don't think there is any other advantage. Startpos that's a different story.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
may I ask why STARTPOS should be so different ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I was just thinking that STARTPOS makes FIND significantly different from INDEX. More powerful.
99 data _null_;
100 input text $50.;
101 putlog 'before:' text=;
102 startpos=find(text,'program','i',1);
103 do until(startpos eq 0);
104 substr(text,startpos,7) = 'PROGRAM';
105 startpos=find(text,'program','i',startpos+7);
106 end;
107 putlog 'during:' text=;
108 text = tranwrd(text,"PROGRAM","programming");
109 putlog 'after: ' text=;
110 putlog ' ';
111 cards;
before:text=This is my program
during:text=This is my PROGRAM
after: text=This is my programming
before:text=PROGRAM is mine
during:text=PROGRAM is mine
after: text=programming is mine
before:text=Change PROgram
during:text=Change PROGRAM
after: text=Change programming
before:text=ThisPrOGrAMworks
during:text=ThisPROGRAMworks
after: text=Thisprogrammingworks
before:text=This program is my program
during:text=This PROGRAM is my PROGRAM
after: text=This programming is my programming
before:text=programprogram program program
during:text=PROGRAMPROGRAM PROGRAM PROGRAM
after: text=programmingprogramming programming programming
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
kumar
your point is well made - there is a useful extension to be implemented here
I think you should suggest it at the SASware ballot - see
https://communities.sas.com/community/support-communities/ballot
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Peter: have I missed something? I understand DN's points that that the modifier and startpos are extremely nice to have with the find function, but what is being suggested for the ballot? To make the index function precisely the same as the find function?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Art
Only the original idea.
Provide TRANWRD with the 'i' modifier.
And while they are about it, they could add something similar for the scanning performed by the INPUT @'string' - probably via an INFILE statement option
Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can use PRXCHANGE instead of TRANWRD, then you can use the "i" modifier to ignore the case in the input string. To convert variable TEXT into variable OUTPUT use:
output = prxchange('s/program/Programming/i',-1,text);
I'll leave as an exercise how to jazz it up so that the case of the P in Programming matches the original word.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm very much with Tom on this one. Not that it couldn't be done without RegEx - but RegEx is just so much more versatile to deal with such cases.
data have;
infile datalines truncover;
input text $100.;
datalines;
This is my program
PROGRAM is mine
Change PROgram
multiprogram vs time sharing
programmatic art
;
run;
data want;
set have;
output = prxchange('s/(p)rogram\b/$1rogramming/i',-1,text);
run;