Hello,
At first sight, it could seems to be an easy question, but i can't solve it.
In a string I could have something like this:
Have: street flowers4 nowhere want: street flowers 4 nowhere
Have: 4Avenue10 nowhere want: 4 Avenue 10 nowhere
Resuming, insert a space when there's a sequence char/numeric or numeric/char.
How can I do this if don't know the position where it may occur?
Thanks in advance!
K
Use regular expressions:
data have;
informat line $64.;
input line &;
datalines;
street flowers4 nowhere
4Avenue10 nowhere
;
data want;
set have;
length newLine $128;
newLine = compbl(prxChange("s/(\d+)/ \1 /o", -1, line));
run;
proc print; run;
Hello @Krauss,
Maybe it's easier to handle the two cases separately, e.g. like this:
data have;
input c $50.;
cards;
street flowers4 nowhere
4Avenue10 nowhere
;
data want;
set have;
c=prxchange('s/([^\d\s])(\d)/$1 $2/', -1, c);
c=prxchange('s/(\d)([^\d\s])/$1 $2/', -1, c);
run;
The regular expression [^\d\s] means "not a digit or whitespace character." You may want to change it to something else, for example to [[:alpha:]] if your definition of "Char" does not include punctuation marks, but only alphabetic characters.
Thank you for your answer.
Use regular expressions:
data have;
informat line $64.;
input line &;
datalines;
street flowers4 nowhere
4Avenue10 nowhere
;
data want;
set have;
length newLine $128;
newLine = compbl(prxChange("s/(\d+)/ \1 /o", -1, line));
run;
proc print; run;
That's exactly what I asked for!
Thank you!
@Krauss wrote:
That's exactly what I asked for!
Not exactly, I'd say: For example, your second test string gets indented by one character because a blank is inserted left to the "4" although there was no "char" justifying this insertion ("insert a space when there's a sequence char/numeric or numeric/char"). But, of course, this is not a big issue and PG's solution is elegant (as always) in that it requires only a single assignment statement.
Yes @FreelanceReinh , good point. I have taken liberties with OP stated requirements. But then, those might not be exactly what is really required! I guess you might want to use left(compbl(...)) if you never want leading spaces.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.