🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lapis Lazuli | Level 10

How to replace the word using array and tranwrd

``````data test1;
length string \$1000;
string= "THIS IS A REALLY LONG STRING THAT I WANT TO SPLIT G LONG ENOUGH TO SPLIT";
array str{4} \$ 50 ('not', 'is', 'a', 'to' ) ;
mid=string;
do i=1 to 4;
y=trim(upcase(str{i}));
x=trim(str{i});
if index(mid,y) then do; mid=tranwrd(mid,y ,x );
put x y mid;
end;
end;
string=mid;
run;``````

my code cannot work, to get the final result as below:

THIS is a REALLY LONG STRING THAT I WANT to SPLIT G LONG ENOUGH to SPLIT

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

Re: How to replace the word using array and tranwrd

``````data test1;
length string want \$1000;
string= "THIS IS A REALLY LONG STRING THAT I WANT TO SPLIT G LONG ENOUGH TO SPLIT";
array str{4} \$ 50 ('not', 'is', 'a', 'to' ) ;
call missing(want);
do i=1 to countw(string,' ');
temp=scan(string,i,' ');
if lowcase(temp) in str then want=catx(' ',want,lowcase(temp));
else want=catx(' ',want,temp);
end;

run;``````
11 REPLIES 11
Lapis Lazuli | Level 10

Re: How to replace the word using array and tranwrd

replace a word a time, this can work, for example, tranwrd(string,"IS" ,"is" );

but it cannot work using array and do-loop, why? how to modify the code?

Calcite | Level 5

Re: How to replace the word using array and tranwrd

Try using trim for variables x and y:

if index(mid,trim(y)) then do;
mid=tranwrd(mid, trim(y), trim(x) );
put x y mid;
end;

Lapis Lazuli | Level 10

Re: How to replace the word using array and tranwrd

Thanks @D_ph14 , but it can get what I want
Super User

Re: How to replace the word using array and tranwrd

``````data test1;
length string want \$1000;
string= "THIS IS A REALLY LONG STRING THAT I WANT TO SPLIT G LONG ENOUGH TO SPLIT";
array str{4} \$ 50 ('not', 'is', 'a', 'to' ) ;
call missing(want);
do i=1 to countw(string,' ');
temp=scan(string,i,' ');
if lowcase(temp) in str then want=catx(' ',want,lowcase(temp));
else want=catx(' ',want,temp);
end;

run;``````
Lapis Lazuli | Level 10

Re: How to replace the word using array and tranwrd

@Ksharp ，I appreciate your help, if the variable has a lot of characters, it will time consuming to combine each word a time using catx function.
Super User

Re: How to replace the word using array and tranwrd

Did you try Patrick's PRXCHANGE() ?
Meteorite | Level 14

Re: How to replace the word using array and tranwrd

You can easily do that using the PRXCHANGE function:

``````data test1;
length string \$1000;
string="THIS IS A REALLY LONG STRING THAT I WANT TO SPLIT G LONG ENOUGH TO SPLIT";
mid=prxchange('s/(\bNOT\b|\bIS\b|\bA\b|\bTO\b)/\L\$1/', -1, string);
run;``````

This code :

1 - Search for the pattern NOT or IS or A or TO (\b identifies a word boundary)

2 - Replace it (\$1) by its lowercase equivalent (lowcase  = \L) as many times as needed (-1 as a second argument)

Opal | Level 21

Re: How to replace the word using array and tranwrd

Same as @ed_sas_member just with a small change to the RegEx

``````data _null_ ;
s = "IS IT POSSIBLE TO LOWCASE WORDS A IS NOT TO IN A LONG STRING USING TRANWRD OR NOT" ;
s=prxchange('s/\b(a|is|not|to)\b/\L\1/oi',-1,trim(s));
put s ;
stop;
run;
``````
Lapis Lazuli | Level 10

Re: How to replace the word using array and tranwrd

Hi @ed_sas_member, thanks for your method using RegEx.

It is very clear with your detailed explanations.
Ammonite | Level 13

Re: How to replace the word using array and tranwrd

Possible. But you need to:

• make sure that only the words in the list are lowcased if surrounded by blanks
• account for the endpoints with one-sided blanks if any

Here's one way:

``````data want (keep = s) ;
s = "IS IT POSSIBLE TO LOWCASE WORDS A IS NOT TO IN A LONG STRING USING TRANWRD OR NOT" ;
array w \$3 w1-w4 ("a" "is" "not" "to") ;
retain b "" ;
do over w ;
s = left (tranwrd (b || s || b, b || upcase (trim (w)) || b, b || trim (w) || b)) ;
end ;
put s ;
run ;
``````

Result:

```is IT POSSIBLE to LOWCASE WORDS a is not to IN a LONG STRING USING TRANWRD OR not
```

Kind regards

Paul D.

Lapis Lazuli | Level 10

Re: How to replace the word using array and tranwrd

Thanks @hashman , my confusion is clear up.
Discussion stats
• 11 replies
• 1931 views
• 4 likes
• 6 in conversation