<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Replacing with prxmatch function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726272#M225689</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286185"&gt;@SASdevAnneMarie&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;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" ?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You're welcome. See the&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p0s9ilagexmjl8n1u7e1t1jfnzlk.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;Tables of Perl Regular Expression (PRX) Metacharacters&lt;/A&gt; to find out:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The caret &lt;FONT face="courier new,courier"&gt;^&lt;/FONT&gt; stands for the beginning of the string &lt;FONT face="courier new,courier"&gt;strip(mt)&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;0*&lt;/FONT&gt; matches zero or more &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;s.&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;.*&lt;/FONT&gt; matches zero or more arbitrary characters (except newline).&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;[^0]&lt;/FONT&gt; matches a single character different from &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;. (Edit: Here, the caret has a different meaning than in the first character of the regular expression.)&lt;/LI&gt;
&lt;LI&gt;Again,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;0*&lt;/FONT&gt; matches zero or more &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;s.&lt;/LI&gt;
&lt;LI&gt;The dollar sign &lt;FONT face="courier new,courier"&gt;$&lt;/FONT&gt;&amp;nbsp;stands for the end of the string &lt;FONT face="courier new,courier"&gt;strip(mt)&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;The replacement&amp;nbsp;&lt;FONT face="courier new,courier"&gt;\1&lt;/FONT&gt; stands for the string matched by the first (in our case: the only) pattern in parentheses (&lt;FONT face="courier new,courier"&gt;.*[^0]&lt;/FONT&gt;).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt; in the second argument of the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0r8h2fa8djqf1n1cnenrvm573br.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;PRXCHANGE function&lt;/A&gt; is the number of times the replacement is to be executed (per call of the function). Here, a single replacement is sufficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Masters of the PRX functions like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;are likely to improve on any given regular expression. His elegant "&lt;FONT face="courier new,courier"&gt;^0+|0+$&lt;/FONT&gt;" matches &lt;EM&gt;one&lt;/EM&gt; or more leading zeros (&lt;FONT face="courier new,courier"&gt;^0+&lt;/FONT&gt;) &lt;EM&gt;or&lt;/EM&gt;&amp;nbsp;(&lt;FONT face="courier new,courier"&gt;|&lt;/FONT&gt;) &lt;EM&gt;one&lt;/EM&gt; or more trailing zeros (&lt;FONT face="courier new,courier"&gt;0+$&lt;/FONT&gt;) and he just removes these matched parts of the string in the result (replacement = empty string). Note that now a &lt;EM&gt;single&lt;/EM&gt; replacement is &lt;EM&gt;not&lt;/EM&gt; 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 &amp;gt;1 or simply equal to &lt;FONT face="courier new,courier"&gt;-1&lt;/FONT&gt; (to continue the replacements until the end of&amp;nbsp;the source string&lt;I&gt;&amp;nbsp;&lt;/I&gt;is reached). Also note that if&amp;nbsp;&lt;FONT face="courier new,courier"&gt;strip(mt)&lt;/FONT&gt;&amp;nbsp;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 (&lt;FONT face="courier new,courier"&gt;[^0]&lt;/FONT&gt;) is found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Mar 2021 13:25:12 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-03-15T13:25:12Z</dc:date>
    <item>
      <title>Replacing with prxmatch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726228#M225663</link>
      <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data is:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_0-1615800322330.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55946i0767313EF32C3146/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_0-1615800322330.png" alt="MarieT_0-1615800322330.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to replace "0" by space before and after the values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mt="0000000302972E" -&amp;gt; Mt_final= 302972E&lt;/P&gt;
&lt;P&gt;Mt="0000000141852A0000" -&amp;gt;Mt-final= 141852A&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code is :&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_2;
	set table_1;
	if prxmatch("m/0/oi",mt)&amp;gt; 0 then
		mt_final=tranwrd(mt,"0","");
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But some times with the "0" inside the value like&amp;nbsp;Mt="00000003&lt;STRONG&gt;0&lt;/STRONG&gt;2972E" it doesn't work because I get Mt_final= 3 2972E, not&amp;nbsp;Mt_final= 302972E.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm wondering if there is some option in prxmatch function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for helping me !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 09:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726228#M225663</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-03-15T09:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing with prxmatch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726231#M225664</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286185"&gt;@SASdevAnneMarie&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Explanation: After removing leading and trailing blanks from &lt;FONT face="courier new,courier"&gt;mt&lt;/FONT&gt; (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.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 10:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726231#M225664</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-15T10:23:53Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing with prxmatch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726237#M225668</link>
      <description>&lt;PRE&gt;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;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Mar 2021 11:27:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726237#M225668</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-03-15T11:27:52Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing with prxmatch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726238#M225669</link>
      <description>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" ?</description>
      <pubDate>Mon, 15 Mar 2021 11:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726238#M225669</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-03-15T11:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing with prxmatch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726272#M225689</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286185"&gt;@SASdevAnneMarie&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;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" ?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You're welcome. See the&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p0s9ilagexmjl8n1u7e1t1jfnzlk.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;Tables of Perl Regular Expression (PRX) Metacharacters&lt;/A&gt; to find out:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The caret &lt;FONT face="courier new,courier"&gt;^&lt;/FONT&gt; stands for the beginning of the string &lt;FONT face="courier new,courier"&gt;strip(mt)&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;0*&lt;/FONT&gt; matches zero or more &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;s.&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;.*&lt;/FONT&gt; matches zero or more arbitrary characters (except newline).&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;[^0]&lt;/FONT&gt; matches a single character different from &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;. (Edit: Here, the caret has a different meaning than in the first character of the regular expression.)&lt;/LI&gt;
&lt;LI&gt;Again,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;0*&lt;/FONT&gt; matches zero or more &lt;FONT face="courier new,courier"&gt;0&lt;/FONT&gt;s.&lt;/LI&gt;
&lt;LI&gt;The dollar sign &lt;FONT face="courier new,courier"&gt;$&lt;/FONT&gt;&amp;nbsp;stands for the end of the string &lt;FONT face="courier new,courier"&gt;strip(mt)&lt;/FONT&gt;.&lt;/LI&gt;
&lt;LI&gt;The replacement&amp;nbsp;&lt;FONT face="courier new,courier"&gt;\1&lt;/FONT&gt; stands for the string matched by the first (in our case: the only) pattern in parentheses (&lt;FONT face="courier new,courier"&gt;.*[^0]&lt;/FONT&gt;).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;1&lt;/FONT&gt; in the second argument of the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0r8h2fa8djqf1n1cnenrvm573br.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;PRXCHANGE function&lt;/A&gt; is the number of times the replacement is to be executed (per call of the function). Here, a single replacement is sufficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Masters of the PRX functions like&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;are likely to improve on any given regular expression. His elegant "&lt;FONT face="courier new,courier"&gt;^0+|0+$&lt;/FONT&gt;" matches &lt;EM&gt;one&lt;/EM&gt; or more leading zeros (&lt;FONT face="courier new,courier"&gt;^0+&lt;/FONT&gt;) &lt;EM&gt;or&lt;/EM&gt;&amp;nbsp;(&lt;FONT face="courier new,courier"&gt;|&lt;/FONT&gt;) &lt;EM&gt;one&lt;/EM&gt; or more trailing zeros (&lt;FONT face="courier new,courier"&gt;0+$&lt;/FONT&gt;) and he just removes these matched parts of the string in the result (replacement = empty string). Note that now a &lt;EM&gt;single&lt;/EM&gt; replacement is &lt;EM&gt;not&lt;/EM&gt; 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 &amp;gt;1 or simply equal to &lt;FONT face="courier new,courier"&gt;-1&lt;/FONT&gt; (to continue the replacements until the end of&amp;nbsp;the source string&lt;I&gt;&amp;nbsp;&lt;/I&gt;is reached). Also note that if&amp;nbsp;&lt;FONT face="courier new,courier"&gt;strip(mt)&lt;/FONT&gt;&amp;nbsp;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 (&lt;FONT face="courier new,courier"&gt;[^0]&lt;/FONT&gt;) is found.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 13:25:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726272#M225689</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-15T13:25:12Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing with prxmatch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726282#M225691</link>
      <description>Thank you very much !&lt;BR /&gt;</description>
      <pubDate>Mon, 15 Mar 2021 13:27:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-with-prxmatch-function/m-p/726282#M225691</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-03-15T13:27:38Z</dc:date>
    </item>
  </channel>
</rss>

