- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It is very clear with your detailed explanations.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content