DATA Step, Macro, Functions and more

Replace string with another string

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Replace string with another string

Hi all,

 

I have a sentence like "I am a student in a college in a city in India to study masters"

 

in the above sentence, I want to replace the second occurrence of "in" with "from".

 

Can anyone suggest me code to replace it.

 

i have tried with the following code, i made it. But am looking for another way which makes easy to execute.

 

 

data x;

word = "I am a student in a college in a city in India to study masters";

substr(word, 29, 2) = "from";

run;

 

Thank you & regards.

Manoj


Accepted Solutions
Solution
3 weeks ago
PROC Star
Posts: 2,316

Re: Replace string with another string

Like this?

NEWWORD=prxchange('s/(.*? in .*?)( in )(.*)/$1 from $3/',-1,WORD);

I hope there's no error, I can't test atm.

View solution in original post


All Replies
PROC Star
Posts: 253

Re: Replace string with another string

You probably want to use the TRANWRD function: 

Word=tranwrd(Word,'to','from');

Contributor
Posts: 22

Re: Replace string with another string

Thank you for replay,

 by using TRANWRD all in 's will be replaced by "from", but i want to replace only second "in" with "from"

 

 

Super User
Super User
Posts: 9,416

Re: Replace string with another string

And what is the logic to assume that?  Simply put, if you had the string:

I am a student in a college in a city in India to study masters

How are we to note that the second in is to be replaced, and not the others?

I mean I could tell you to substr or scan the string, but that would only help in this one specific case.

 

Solution
3 weeks ago
PROC Star
Posts: 2,316

Re: Replace string with another string

Like this?

NEWWORD=prxchange('s/(.*? in .*?)( in )(.*)/$1 from $3/',-1,WORD);

I hope there's no error, I can't test atm.

Occasional Contributor
Posts: 15

Re: Replace string with another string

DATA TEMP;
word = "I am a student in a college in a city in INDIA to study masters";

word_len= LENGTH(STRIP(word));
fst_pos= FINDW(STRIP(word),'in');
snd_pos= FINDW(STRIP(word),'in',fst_pos+1);

new_str_part1= substr(STRIP(word),1,snd_pos-1);
new_str_part2= 'from';
new_str_part3= substr(STRIP(word),snd_pos+2);

new_word= CAT(new_str_part1,new_str_part2,new_str_part3);

DROP word_len fst_pos snd_pos new_str_part:;
run;

 

PROC PRINT DATA=TEMP;
RUN;

Contributor
Posts: 22

Re: Replace string with another string

Posted in reply to mahesh146
Thank you, this one worked
Super User
Posts: 10,689

Re: Replace string with another string

@ChrisNZ

Can you explain the red one stand for what ?

 

prxchange('s/(.*? in .*?)( in )(.*)/

Super User
Posts: 10,689

Re: Replace string with another string

[ Edited ]
data x;
word = "I am a student in a college in a city in India to study masters    ";
put word= ;

pid=prxparse('/\bin\b/i');
s=1;e=length(word);
call prxnext(pid,s,e,word,p,l);
n=0;
do while(p>0);
 n+1;
 if n=2 then do;want=substr(word,1,p-1)||'from'||substr(word,p+l);leave;end;
 call prxnext(pid,s,e,word,p,l);
end;
put want= ;
run;
Contributor
Posts: 22

Re: Replace string with another string

Thank you this one worked
Respected Advisor
Posts: 3,167

Re: Replace string with another string

You already have your answer, both answers from @ChrisNZ and @Ksharp should work. I am here just to comment that your original example code will not work for your purpose. The reason being SAS using FIXED length variable, you will need byte abundancy or some walk-around (rename etc.) to replace with a longer chars, notice @Ksharp's example with some blanks padded.  

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 191 views
  • 5 likes
  • 7 in conversation