Please give some more examples of input and output.
You can loop across words like in:
c1 = "heis walkingwalking";
do i=1 to countw(c1);
word = scan(c1,i);
put i= word=;
end;
then check each word does it contain pair number of characters then check is first half same as second half
- using length() and substr() functions.
But what about "aa" - is it a double "a" ? or "walkingwalkimg" a double word with some typo ?
As to the second request you need check like:
c1 = 'srt abc- rty 50987';
do i=1 to countw(c1);
word = scan(c1,i);
if word='abc' then do;
tmp = combl(c1,'kd'); /* compress blank, keep digits */
if tmp ne ' ' then
do j=1 to (coutw(tmo));
num_str = scan(tmp,j);
if length(numstr) = 5 and
index(c1,numstr) > index(c1,'abc')
then wanted = substr(c1,index(c1));
end;
end;
Try to create a test code of above. In case of issues post
your code, the log and point what issues you have.
Regular Expressions are what you're looking for when it comes to dealing with text patterns.
I wasn't really sure about your 2nd pattern: Is this pattern now require as first word abc or any string that starts with abc? Given the dash you've had in your first sample I've been going for the 2nd option - and string starting with abc.
data have;
infile datalines truncover;
input str $255.;
datalines;
he is walkingwalking ww walkingwalkings
ron was sleeping at thatthat time
srt abc- rty 50987
ftu ght abc trying 76543
;
run;
data want;
set have;
str_new=str;
/* add blank between repeated string of at least two characters per repetition */
str_new=prxchange('s/\b(\w{2,99})(\1)\b/\1 \2/oi',-1,strip(str_new));
/* extract pattern */
str_new=prxchange('s/^.*(\babc\S*\s+\w+\s+\d{5}\b).*$/\1/oi',-1,strip(str_new));
run;
The metacharacters used in the RegEx are documented here:
I would simplify to:
data want;
set have;
/* add blank between repeated string of at least two characters per repetition */
str_new=prxchange('s/\b(\w{2,})\1\b/\1 \1/oi', -1, str);
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.