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?
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.
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;
Unfortunately, the whole string is character.
@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
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.
Won't work with values like "111green222", but only @luvscandy27 knows whether values with multiple numbers exist.
data have;
input var :$25.;
datalines;
4red
56blue
118white
20022orange
;
run;
data want;
set have;
want = prxchange('s/(\d+)/\1 /',1,var);
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: