Hi Experts,
I have a dataset with values as follows:
abc def123 ftr kkkg34
def 123red der45 ffr 45
What I want here is to seperate number and characte with space delimeter
so my result would be something like this
abc def 123 ftr kkkg 34
def 123 red der 45 ffr 45
Regards,
Rohit
So you don't want split the string when this string only contains one digit ?
data have;
str = 'abc def123 ftr kkkg3 a'; output;
str = 'def 123red der5 ffr 45'; output;
run;
data want;
set have;
length newstr $80;
newstr=compbl(prxchange('s/(\d\d+)/ $1 /i',-1,str));
run;
Hi @Rohit_1990
There are probably more elegant ways with prxchange, but this works too:
data want (keep=str newstr); set have;
length w1 w2 $20 newstr $60;
do i = 1 to countw(str);
w1 = scan(str,i,' ');
pos1 = prxmatch('/\d\D|\D\d/',trim(w1));
if pos1 > 1 then w2 = catx(' ',substr(w1,1,pos1),substr(w1,pos1+1));
else w2 = w1;
newstr=catx(' ',newstr,w2);
end;
run;
newstr=abc def 123 ftr kkkg 34
newstr=def 123 red der 45 ffr 45
I gave it a try and found a solution with one prxchange to handle the CN boundaries and another to handle the NC boundaries, and it is nicer than the loop-method, I assume that both boundary-types can be handled in the same expression, but I cannot figure out how, and I hope that sombody will supply the final one-liner.
The problem is to find both CN and NC-boundaries and also split them in C N or N C to insert a space in the same expression.
data have;
str = 'abc def123 ftr kkkg34 a'; output;
str = 'def 123red der45 ffr 45'; output;
run;
data want; set have;
length newstr $80;
newstr=prxchange('s/\b([A-Z]+)([0-9]+\b\s)/$1 $2/i',-1,str);
newstr=prxchange('s/\b([0-9]+)([A-Z]+\b\s)/$1 $2/i',-1,newstr);
put newstr;
run;
abc def 123 ftr kkkg 34 a
def 123 red der 45 ffr 45
data have;
str = 'abc def123 ftr kkkg34 a'; output;
str = 'def 123red der45 ffr 45'; output;
run;
data want;
set have;
length newstr $80;
newstr=compbl(prxchange('s/(\d+)/ $1 /i',-1,str));
put newstr;
run;
proc print;run;
Hi Ksharp,
Your answer is perfect just that it fails when alphanumeric string contains single digit.
data have;
str = 'abc def123 ftr kkkg3 a'; output;
str = 'def 123red der5 ffr 45'; output;
run;
Can you please suggest on above.
Thanks a ton for your great suggetion.
Also can you suggest any documents/attchement to explain more on prx function nad Hash programme in sas.
Regards,
Anand
So you don't want split the string when this string only contains one digit ?
data have;
str = 'abc def123 ftr kkkg3 a'; output;
str = 'def 123red der5 ffr 45'; output;
run;
data want;
set have;
length newstr $80;
newstr=compbl(prxchange('s/(\d\d+)/ $1 /i',-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.