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

I have a dataset with a char variable containing the following example values:

AA1

AA2a

AA10

AA11a

BB3

BB3b

BB10

BB11b

 

Essentially I want to zero-pad the numeric part of each value (e.g. 'AA2a' becomes 'AA02a') and thought the prxchange function would be a quick and simple way of achieving this.  However, the problem I have run into is how to reference the zero in the replacement text.

So far the code is similar to:

var2 = prxchange('s/^(\w{2})(\d{1})(\D?)$/$1$2$3/', -1, trim(var1));

 

How do I reference (and delimit) the zero character in the replacement string of the prxchange function ($1 ->0<- $2$3)?

1 ACCEPTED SOLUTION

Accepted Solutions
Maurits
Fluorite | Level 6

Never mind, I've managed to find the solution whilst scrawling the interweb! Smiley Very Happy

 

For those that are interested... use the perl metacharacter \Q which escapes all non-word characters.

 

So, the code becomes:

var2 = prxchange('s/^(\w{2})(\d{1})(\D?)$/$1\Q0$2$3/', -1, trim(var1));

View solution in original post

4 REPLIES 4
Maurits
Fluorite | Level 6

Never mind, I've managed to find the solution whilst scrawling the interweb! Smiley Very Happy

 

For those that are interested... use the perl metacharacter \Q which escapes all non-word characters.

 

So, the code becomes:

var2 = prxchange('s/^(\w{2})(\d{1})(\D?)$/$1\Q0$2$3/', -1, trim(var1));

Ksharp
Super User
data have;
input x $;
y=prxchange('s/^([a-z]+)(\d)([a-z]*)$/\10\2\3/i',1,strip(x));
cards;
AA1
AA2a
AA10
AA11a
BB3
BB3b
BB10
BB11b
;
run;
Maurits
Fluorite | Level 6

Thanks for the reply Ksharp but your proposal wouldn't work as the prxchange function would look for the input buffer "\10".

Ksharp
Super User
But there are only three buffer.
Did you run my code and see the output?

or try   $10


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 4 replies
  • 773 views
  • 0 likes
  • 2 in conversation