<?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: How to remove the last '_' and number? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373127#M89252</link>
    <description>&lt;P&gt;Alternatively with perl regular expression&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
input Names:$100.;
names=prxchange('s/\_\d+//',-1,compress(names));
put names=;
datalines;
dx_1
dx_10
state_id_5
state_id_9
if_mg_hk_4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 05 Jul 2017 02:16:29 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2017-07-05T02:16:29Z</dc:date>
    <item>
      <title>How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373119#M89249</link>
      <description>&lt;P&gt;Hello:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to remove the last '_' and number from the strings below.&amp;nbsp; Please advice how.&amp;nbsp; Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; test1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; Names:&lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;$100.&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;dx_1&lt;/P&gt;&lt;P&gt;dx_10&lt;/P&gt;&lt;P&gt;state_id_5&lt;/P&gt;&lt;P&gt;state_id_9&lt;/P&gt;&lt;P&gt;if_mg_hk_4&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;I am looking for the results is like below.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;list&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;dx&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;dx&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;state_id&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;state_id&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;if_mg_hk&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 01:43:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373119#M89249</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-07-05T01:43:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373120#M89250</link>
      <description>&lt;P&gt;Bit of a long way around, but find out the length of the last component which will be 2 or 3 it seems and then use SUBSTR to extract from 1 to the length of the string - 2/3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
    input Names:$100.;

    last=scan(names, -1, "_");
    word=substr(names, 1, length(names)-length(last)-1);

    datalines;
dx_1
dx_10
state_id_5
state_id_9
if_mg_hk_4
;
run;

proc print ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jul 2017 01:46:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373120#M89250</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-05T01:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373127#M89252</link>
      <description>&lt;P&gt;Alternatively with perl regular expression&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
input Names:$100.;
names=prxchange('s/\_\d+//',-1,compress(names));
put names=;
datalines;
dx_1
dx_10
state_id_5
state_id_9
if_mg_hk_4
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Jul 2017 02:16:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373127#M89252</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-07-05T02:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373130#M89255</link>
      <description>&lt;P&gt;What is the '\_\d+/' refering to?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 02:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373130#M89255</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-07-05T02:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373137#M89260</link>
      <description>&lt;P&gt;Regular expression is kind of seperate &amp;nbsp;language in SAS having its own syntax. This paper has nice introduction of &amp;nbsp;it.&lt;/P&gt;&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi29/265-29.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi29/265-29.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 03:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373137#M89260</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2017-07-05T03:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373143#M89262</link>
      <description>&lt;P&gt;\_ and \d are metacharacters&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;\_ : recognizes the underscore( _ )&lt;/P&gt;
&lt;P&gt;\d : recognizes the digit (0-9)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;+ : recognize the charcater/digit one or more times&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;\_\d+ : recognize the underscore followed by digit (one of more time)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;// : replace the metacharacters identified and make them blank.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 04:25:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373143#M89262</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-07-05T04:25:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373187#M89277</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/67134"&gt;@ybz12003&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Use the syntax which returns&amp;nbsp;the result you want and which is easiest for you to understand and maintain.&lt;/P&gt;
&lt;P&gt;Below 3 coding version (word1 to word3) for you to pick and choose.&lt;/P&gt;
&lt;P&gt;The sample code and output hopefully also illustrates that it's really important to provide "nasty" sample data which covers as many cases as you can think of.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  infile datalines truncover;
  input Names:$100.;
  length word1 word2 word3 last $100;

  last=scan(names, -1, "_");
  word1=substr(names, 1, length(names)-length(last)-1);
  word2=substr(names, 1, findc(names,'_',-length(names))-1);
  word3=prxchange('s/_\d+\s*$//oi',1,names);
  datalines;
dx_1
dx_10
state_id_5
state_id_9
if_mg_hk_4
if_mg_hk_
if_mg_hk
if
if_mg_5_hk_4
if_mg_5_hk_
if_mg_5_hk
 
_5_tt
_5tt
 
9_5_tt
_9_5tt
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10126iBA1A97F726C1F415/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="Capture.JPG" title="Capture.JPG" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 09:43:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373187#M89277</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-05T09:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373217#M89282</link>
      <description>&lt;P&gt;Thanks for all of your kind help, and great explaination.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 11:51:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373217#M89282</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-07-05T11:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373270#M89291</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/67134"&gt;@ybz12003&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Even though the solution you've accepted returns the desired result for you sample data, I believe the RegEx needs a small amendment to not cause issues for your real data.&lt;/P&gt;
&lt;P&gt;I suggest you add a &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;$&lt;/FONT&gt;&lt;/STRONG&gt; to the RegEx to ensure that it only removes _&amp;lt;digits&amp;gt; at the end of a string.&lt;/P&gt;
&lt;P&gt;The $ stands for "end of string"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also replace the &lt;FONT color="#FF0000"&gt;-1&lt;/FONT&gt; with a &lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;-1&lt;/FONT&gt; means that the pattern gets searched and replaces as many time as it's found. You should have only a single matching pattern at maximum so using a 1 without the minus appears appropriate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;names=prxchange('s/\_\d+&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;$&lt;/FONT&gt;&lt;/STRONG&gt;//',&lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;,&lt;FONT color="#ff0000"&gt;trim&lt;/FONT&gt;(names));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without the $ ALL &lt;EM&gt;_&amp;lt;digits&amp;gt;&lt;/EM&gt; patterns would get removed and a source string like ABC_5_XX_10 would become ABC_XX&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And instead of compress() use trim()&lt;/P&gt;
&lt;P&gt;Compress removes all blanks from a string - also the ones in the middle of the string - which potentially changes your source string more than what you have been asking for. Trim() only removes trailing blanks which ensures that after the last _&amp;lt;digits&amp;gt; pattern there won't be any blanks before the &amp;lt;end of string&amp;gt; $&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively the RegEx could also look like:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;names=prxchange('s/\_\d+&lt;FONT color="#FF0000"&gt;\s*&lt;/FONT&gt;&lt;/SPAN&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;$&lt;/FONT&gt;&lt;/STRONG&gt;&lt;SPAN&gt;//',&lt;/SPAN&gt;&lt;FONT color="#FF0000"&gt;1&lt;/FONT&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;names);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;\s* represents 0 to many blanks and though you don't have to remove the trailing blanks anymore as now the RegEx caters for such a case.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 13:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373270#M89291</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-05T13:47:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to remove the last '_' and number?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373275#M89292</link>
      <description>&lt;P&gt;Thanks for your detail explaination, Patrick.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 13:46:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-remove-the-last-and-number/m-p/373275#M89292</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2017-07-05T13:46:20Z</dc:date>
    </item>
  </channel>
</rss>

