BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
luvscandy27
Quartz | Level 8

Hi all, 

I thought this question would be easy but I can't seem to find the answer. 

Say I have data like below:

4red

56blue

118white 

20022orange 

 

And I would like to add a space after the numeric portion so my data looks likes this:

4 red

56 blue

118 white 

20022 orange

 

How could I achieve this? 

1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ
data have;
input var :$25.;
datalines;
4red
56blue
118white
20022orange
;
run;

data want;
	set have;
	f = catx(" ", compress(var, , 'kd'), compress(var, , 'ka'));
run;

If length is an issue, make sure to use a LENGTH statement before the CATX statement.

maguiremq_0-1652361338122.png

 

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

If the numbers are always integers (are they?) then you can use the NOTDIGIT function

 

data want;
    set have;
    length newtext $ 24;
    where=notdigit(text);
    newtext=substr(text,1,where-1)||' '||substr(text,where);
run;
--
Paige Miller
luvscandy27
Quartz | Level 8

Unfortunately, the whole string is character. 

PaigeMiller
Diamond | Level 26

@luvscandy27 wrote:

Unfortunately, the whole string is character. 


That's not what I meant by "integer". In your examples

 

4red

56blue

118white 

20022orange 

 

all of the digits turn into integers. There are no non-integers such as

 

20.333orange

--
Paige Miller
maguiremq
SAS Super FREQ
data have;
input var :$25.;
datalines;
4red
56blue
118white
20022orange
;
run;

data want;
	set have;
	f = catx(" ", compress(var, , 'kd'), compress(var, , 'ka'));
run;

If length is an issue, make sure to use a LENGTH statement before the CATX statement.

maguiremq_0-1652361338122.png

 

andreas_lds
Jade | Level 19

Won't work with values like "111green222", but only @luvscandy27 knows whether values with multiple numbers exist.

Ksharp
Super User
data have;
input var :$25.;
datalines;
4red
56blue
118white
20022orange
;
run;

data want;
	set have;
	want = prxchange('s/(\d+)/\1 /',1,var);
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 687 views
  • 2 likes
  • 5 in conversation