DATA Step, Macro, Functions and more

Avoiding case sensitive in TRANWRD function.

Reply
Contributor
Posts: 30

Avoiding case sensitive in TRANWRD function.

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.
   

PROC Star
Posts: 7,492

Re: Avoiding case sensitive in TRANWRD function.

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;

Contributor
Posts: 30

Re: Avoiding case sensitive in TRANWRD function.

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


PROC Star
Posts: 7,492

Re: Avoiding case sensitive in TRANWRD function.

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;

Valued Guide
Posts: 765

Re: Avoiding case sensitive in TRANWRD function.

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;

Respected Advisor
Posts: 3,799

Re: Avoiding case sensitive in TRANWRD function.

You can include FIND function modifier "i" which should allow you to remove the UPCASE funtion.

PROC Star
Posts: 7,492

Re: Avoiding case sensitive in TRANWRD function.

Posted in reply to data_null__

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;

Respected Advisor
Posts: 3,799

Re: Avoiding case sensitive in TRANWRD function.

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.

Valued Guide
Posts: 2,177

Re: Avoiding case sensitive in TRANWRD function.

Posted in reply to data_null__

may I ask why STARTPOS should be so different ?

Respected Advisor
Posts: 3,799

Re: Avoiding case sensitive in TRANWRD function.

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

Valued Guide
Posts: 2,177

Re: Avoiding case sensitive in TRANWRD function.

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

PROC Star
Posts: 7,492

Re: Avoiding case sensitive in TRANWRD function.

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?

Valued Guide
Posts: 2,177

Re: Avoiding case sensitive in TRANWRD function.

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

Super User
Super User
Posts: 7,079

Re: Avoiding case sensitive in TRANWRD function.

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.

Respected Advisor
Posts: 4,173

Re: Avoiding case sensitive in TRANWRD function.

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;

Ask a Question
Discussion stats
  • 14 replies
  • 1645 views
  • 0 likes
  • 7 in conversation