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

Hello Experts,

 

My data is:  

MarieT_0-1615800322330.png

 

I would like to replace "0" by space before and after the values:

 

Mt="0000000302972E" -> Mt_final= 302972E

Mt="0000000141852A0000" ->Mt-final= 141852A

 

My code is : 

 

data table_2;
	set table_1;
	if prxmatch("m/0/oi",mt)> 0 then
		mt_final=tranwrd(mt,"0","");
run;

But some times with the "0" inside the value like Mt="0000000302972E" it doesn't work because I get Mt_final= 3 2972E, not Mt_final= 302972E.

 

I'm wondering if there is some option in prxmatch function.

 

Thank you for helping me !

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @SASdevAnneMarie,

 

Try this:

data have;
input mt $20.;
cards; 
0000000302972E
0000000141852A0000
141852A0000
0014000001852A0
141852A
0014001800502A0
000000000000000
;

data want;
set have;
length mt_final $20;
mt_final=prxchange('s/^0*(.*[^0])0*$/\1/', 1, strip(mt));
run;

Explanation: After removing leading and trailing blanks from mt (STRIP function) search for the three-part pattern "leading zeros (if any), a sequence of arbitrary characters ending with a non-zero character, trailing zeros (if any)" and replace it by that "middle" part.

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hello @SASdevAnneMarie,

 

Try this:

data have;
input mt $20.;
cards; 
0000000302972E
0000000141852A0000
141852A0000
0014000001852A0
141852A
0014001800502A0
000000000000000
;

data want;
set have;
length mt_final $20;
mt_final=prxchange('s/^0*(.*[^0])0*$/\1/', 1, strip(mt));
run;

Explanation: After removing leading and trailing blanks from mt (STRIP function) search for the three-part pattern "leading zeros (if any), a sequence of arbitrary characters ending with a non-zero character, trailing zeros (if any)" and replace it by that "middle" part.

SASdevAnneMarie
Barite | Level 11
Hello, thank you very much, that works! I never use the prxchange before. Could you explain me please : leading zeros (if any): is it .*[^0] ? The sequence of arbitrary characters ending with a non-zero character: is it 0*$ ? trailing zeros (if any) ^0* ? Replace it by that "middle" part - is it "1" ?
FreelanceReinh
Jade | Level 19

@SASdevAnneMarie wrote:
Hello, thank you very much, that works! I never use the prxchange before. Could you explain me please : leading zeros (if any): is it .*[^0] ? The sequence of arbitrary characters ending with a non-zero character: is it 0*$ ? trailing zeros (if any) ^0* ? Replace it by that "middle" part - is it "1" ?

You're welcome. See the Tables of Perl Regular Expression (PRX) Metacharacters to find out:

  • The caret ^ stands for the beginning of the string strip(mt).
  • 0* matches zero or more 0s.
  • .* matches zero or more arbitrary characters (except newline).
  • [^0] matches a single character different from 0. (Edit: Here, the caret has a different meaning than in the first character of the regular expression.)
  • Again, 0* matches zero or more 0s.
  • The dollar sign $ stands for the end of the string strip(mt).
  • The replacement \1 stands for the string matched by the first (in our case: the only) pattern in parentheses (.*[^0]).

The 1 in the second argument of the PRXCHANGE function is the number of times the replacement is to be executed (per call of the function). Here, a single replacement is sufficient.

 

Masters of the PRX functions like @Ksharp are likely to improve on any given regular expression. His elegant "^0+|0+$" matches one or more leading zeros (^0+) or (|) one or more trailing zeros (0+$) and he just removes these matched parts of the string in the result (replacement = empty string). Note that now a single replacement is not sufficient because if both leading and trailing zeros are found, the first replacement will only remove the leading blanks. Therefore the second argument of PRXCHANGE must be a number >1 or simply equal to -1 (to continue the replacements until the end of the source string is reached). Also note that if strip(mt) is just a sequence of zeros (see last observation in dataset HAVE) Ksharp's regular expression creates a blank string, whereas mine leaves the zeros unchanged because no non-zero character ([^0]) is found.

 

SASdevAnneMarie
Barite | Level 11
Thank you very much !
Ksharp
Super User
data have;
input mt $20.;
cards; 
0000000302972E
0000000141852A0000
141852A0000
0014000001852A0
141852A
0014001800502A0
000000000000000
;

data want;
set have;
length mt_final $20;
mt_final=prxchange('s/^0+|0+$//', -1, strip(mt));
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 1403 views
  • 4 likes
  • 3 in conversation