BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rohit_1990
Calcite | Level 5

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

View solution in original post

8 REPLIES 8
Peter_C
Rhodochrosite | Level 12
Think regular expression
Prxchange()
ErikLund_Jensen
Rhodochrosite | Level 12

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

 

Peter_C
Rhodochrosite | Level 12
Isn't there a trigger to repeat the regex until exhausted?
That saves the countw() and loop
ErikLund_Jensen
Rhodochrosite | Level 12

@Peter_C 

 

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

 

 

Ksharp
Super User
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;
Rohit_1990
Calcite | Level 5

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

Ksharp
Super User

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;
Rohit_1990
Calcite | Level 5
Thanks a lot !!!!!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1322 views
  • 3 likes
  • 4 in conversation