BookmarkSubscribeRSS Feed
keen_sas
Quartz | Level 8

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.
   

14 REPLIES 14
art297
Opal | Level 21

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;

keen_sas
Quartz | Level 8

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


art297
Opal | Level 21

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;

MikeZdeb
Rhodochrosite | Level 12

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;

data_null__
Jade | Level 19

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

art297
Opal | Level 21

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;

data_null__
Jade | Level 19

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.

Peter_C
Rhodochrosite | Level 12

may I ask why STARTPOS should be so different ?

data_null__
Jade | Level 19

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

Peter_C
Rhodochrosite | Level 12

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

art297
Opal | Level 21

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?

Peter_C
Rhodochrosite | Level 12

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

Tom
Super User Tom
Super User

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.

Patrick
Opal | Level 21

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 6632 views
  • 0 likes
  • 7 in conversation