BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
s_manoj
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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

10 REPLIES 10
s_lassen
Meteorite | Level 14

You probably want to use the TRANWRD function: 

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

s_manoj
Quartz | Level 8

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"

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

ChrisNZ
Tourmaline | Level 20

Like this?

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

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

mahesh146
Obsidian | Level 7

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;

s_manoj
Quartz | Level 8
Thank you, this one worked
Ksharp
Super User

@ChrisNZ

Can you explain the red one stand for what ?

 

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

Ksharp
Super User
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;
s_manoj
Quartz | Level 8
Thank you this one worked
Haikuo
Onyx | Level 15

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.  

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
  • 10 replies
  • 12319 views
  • 6 likes
  • 7 in conversation