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
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;
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?
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;
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;
Hi @blueskyxyz
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)
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;
Possible. But you need to:
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.