BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Krauss
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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;
PG

View solution in original post

6 REPLIES 6
FreelanceReinh
Jade | Level 19

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.

 

 

 

 

Krauss
Fluorite | Level 6

Thank you for your answer.

PGStats
Opal | Level 21

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;
PG
Krauss
Fluorite | Level 6

That's exactly what I asked for!

 

Thank you!

FreelanceReinh
Jade | Level 19

@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.

PGStats
Opal | Level 21

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.

PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1438 views
  • 0 likes
  • 3 in conversation