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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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