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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.