<?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: using array to remove substrings from strings in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839167#M331810</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_set ;
infile datalines truncover; 
input policy_number $100. ; 
want=prxchange('s/MANUALENTRY|MANUAL|CEDEDMANUAL|MANUALCEDED//i',-1,compress(policy_number,'_','s'));
datalines; 
Q2 2017 Manual Entry 
000123400 
Manual 
Manual_fix15 
Manual_fix22Q1 
Manual All Data 
0098765401CededManual 
0098765401Manual 
0011223301 CededManual 
; 
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 18 Oct 2022 11:44:38 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-10-18T11:44:38Z</dc:date>
    <item>
      <title>using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839058#M331744</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I'm trying to remove any key words ('MANUAL', 'MANUALENTRY', 'CEDEDMANUAL', 'MANUALCEDED') from a string (policy_number).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_set ;&lt;/CODE&gt;&lt;BR /&gt;&lt;CODE class=" language-sas"&gt;infile datalines truncover; &lt;BR /&gt;input policy_number $100. ; &lt;BR /&gt;datalines; &lt;BR /&gt;Q2 2017 Manual Entry &lt;BR /&gt;000123400 &lt;BR /&gt;Manual &lt;BR /&gt;Manual_fix15 &lt;BR /&gt;Manual_fix22Q1 &lt;BR /&gt;Manual All Data &lt;BR /&gt;0098765401CededManual &lt;BR /&gt;0098765401Manual &lt;BR /&gt;0011223301 CededManual &lt;BR /&gt;; &lt;BR /&gt;run;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data chk;
  set test_set;

	retain new_policy_number ;
	
	array nn[*] $ _all_ ('MANUALENTRY', 'MANUAL', 'CEDEDMANUAL', 'MANUALCEDED');

	do i=1 to dim(nn);

		new_policy_number = tranwrd(upcase(compress(tranwrd(policy_number,'_',''))), nn[i], "") ;

	end;

	drop i;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are the results:&lt;/P&gt;
&lt;TABLE width="341"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="166"&gt;policy_number&lt;/TD&gt;
&lt;TD width="175"&gt;new_policy_number&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Q2 2017 Manual Entry&lt;/TD&gt;
&lt;TD&gt;Q22017MANUALENTRY&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;00123400&lt;/TD&gt;
&lt;TD&gt;00123400&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Manual&lt;/TD&gt;
&lt;TD&gt;MANUAL&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Manual_fix15&lt;/TD&gt;
&lt;TD&gt;MANUALFIX15&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Manual_fix22Q1&lt;/TD&gt;
&lt;TD&gt;MANUALFIX22Q1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Manual All Data&lt;/TD&gt;
&lt;TD&gt;MANUALALLDATA&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0098765401CededManual&lt;/TD&gt;
&lt;TD&gt;0098765401CEDEDMANUAL&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0098765401Manual&lt;/TD&gt;
&lt;TD&gt;0098765401MANUAL&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;0011223301 CededManual&lt;/TD&gt;
&lt;TD&gt;0011223301CEDEDMANUAL&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;None of the substrings were removed. Here's what they should be:&lt;/P&gt;
&lt;TABLE width="419px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;policy_number&lt;/TD&gt;
&lt;TD width="139.675px"&gt;Should be&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;Q2 2017 Manual Entry&lt;/TD&gt;
&lt;TD width="139.675px"&gt;Q22017&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;00123400&lt;/TD&gt;
&lt;TD width="139.675px"&gt;00123400&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;Manual&lt;/TD&gt;
&lt;TD width="139.675px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;Manual_fix15&lt;/TD&gt;
&lt;TD width="139.675px"&gt;FIX15&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;Manual_fix22Q1&lt;/TD&gt;
&lt;TD width="139.675px"&gt;FIX22Q1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;Manual All Data&lt;/TD&gt;
&lt;TD width="139.675px"&gt;ALLDATA&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;0098765401CededManual&lt;/TD&gt;
&lt;TD width="139.675px"&gt;0098765401&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;0098765401Manual&lt;/TD&gt;
&lt;TD width="139.675px"&gt;0098765401&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="278.325px"&gt;0011223301 CededManual&lt;/TD&gt;
&lt;TD width="139.675px"&gt;0011223301&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought that "new_policy_number" might be overwritten with each iteration, so I tried creating 'i' different (4) new_policy_number variables for each element of the array. That didn't work either. Please let me know what you think. Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Oct 2022 00:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839058#M331744</guid>
      <dc:creator>kb011235</dc:creator>
      <dc:date>2022-10-18T00:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839117#M331779</link>
      <description>&lt;P&gt;Some corrections:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The word "manual" is part of "manualentry" and "cedemanual", so if "manual" is removed first, "cede" and "entry" would stay in the string.&lt;/LI&gt;
&lt;LI&gt;Using "_all_" in the array statement is not what you want, as it includes "policy_number" also.&lt;/LI&gt;
&lt;LI&gt;Upcase and removing blanks and underscores was moved before the loop.&lt;/LI&gt;
&lt;LI&gt;Using "policy_number" in the loops restores already removed words.&lt;/LI&gt;
&lt;LI&gt;Changed from tranwrd to transtrn (see docs) and added trim to get rid of trailing blanks.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set test_set;

   length new_policy_string $ 100;
   array words[4] $ 15 _temporary_ ('MANUALENTRY', 'CEDEDMANUAL', 'MANUALCEDED', 'MANUAL');
   
   new_policy_number = compress(transtrn(upcase(policy_number), '_', ''));
   
   do i = 1 to dim(words);
      new_policy_number = transtrn(new_policy_number, trim(words[i]), '');
   end;
   
   drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Oct 2022 06:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839117#M331779</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-10-18T06:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839120#M331780</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;...&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT face="Arial, Helvetica, sans-serif" color="#333333"&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;/FONT&gt; 
   new_policy_number = compress(transtrn(upcase(policy_number), '_', ''));&lt;BR /&gt;...
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;Did you mean:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;new_policy_number = compress(upcase(policy_number), '_ ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words: remove the underscores and the spaces.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Oct 2022 06:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839120#M331780</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-18T06:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839124#M331781</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;you need to stop the loop after a replacement is made and stop overwriting new_policy_number over and over again until you try to replace with 'MANUALCEDED' which always fails.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_set ;
infile datalines truncover; 
input policy_number $100. ; 
datalines; 
Q2 2017 Manual Entry
000123400
Manual
Manual_fix15
Manual_fix22Q1
Manual All Data
0098765401CededManual
0098765401Manual
0011223301 CededManual
; 
run; 

data chk;
  set test_set;
   length new_policy_number $100;
	retain new_policy_number ;
	
	array nn[4] $50 _temporary_ ('MANUALENTRY' 'MANUAL' 'CEDEDMANUAL' 'MANUALCEDED');

	do i=1 to dim(nn);
      new_policy_number = upcase(compress(tranwrd(policy_number,'_','')));
      if index(new_policy_number,nn[i]) then do;
         new_policy_number = tranwrd(new_policy_number,nn[i], '') ;
         leave;
      end;
	end;

	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please also note that in obs #7 you're first replacing with manual since it comes first in your array and not 'cededmanual'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Oct 2022 06:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839124#M331781</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-10-18T06:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839126#M331783</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;...&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT face="Arial, Helvetica, sans-serif" color="#333333"&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;/FONT&gt; 
   new_policy_number = compress(transtrn(upcase(policy_number), '_', ''));&lt;BR /&gt;...
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;Did you mean:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;new_policy_number = compress(upcase(policy_number), '_ ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In other words: remove the underscores and the spaces.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Well, yes ..&lt;span class="lia-unicode-emoji" title=":worried_face:"&gt;😟&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Oct 2022 06:55:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839126#M331783</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-10-18T06:55:24Z</dc:date>
    </item>
    <item>
      <title>Re: using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839167#M331810</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_set ;
infile datalines truncover; 
input policy_number $100. ; 
want=prxchange('s/MANUALENTRY|MANUAL|CEDEDMANUAL|MANUALCEDED//i',-1,compress(policy_number,'_','s'));
datalines; 
Q2 2017 Manual Entry 
000123400 
Manual 
Manual_fix15 
Manual_fix22Q1 
Manual All Data 
0098765401CededManual 
0098765401Manual 
0011223301 CededManual 
; 
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Oct 2022 11:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839167#M331810</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-10-18T11:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839227#M331838</link>
      <description>&lt;P&gt;Your solution is close. As Andreas_ids points out, "MANUAL" should be checked last. Wrap it in an upcase and that will work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What's interesting is that case doesn't matter to the&amp;nbsp;prxchange function; the key words are all caps and the string is not capitalized, yet the key words are removed from the string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Follow-up question regarding prxchange; there are a couple of other operations performed on policy_number to remove leading zeros and any characters that are not alphanumeric.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;prxchange('s/^0+//o',-1,prxchange('s/[^A-Z 0-9]//i',-1,policy_number))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I tried combining the two prxchange operations into one but couldn't get it to work. Can the two prxchange statements be combined into one?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 18 Oct 2022 16:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839227#M331838</guid>
      <dc:creator>kb011235</dc:creator>
      <dc:date>2022-10-18T16:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839229#M331839</link>
      <description>Thanks everyone for your feedback. I appreciate it!</description>
      <pubDate>Tue, 18 Oct 2022 16:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839229#M331839</guid>
      <dc:creator>kb011235</dc:creator>
      <dc:date>2022-10-18T16:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: using array to remove substrings from strings</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839347#M331889</link>
      <description>&lt;P&gt;Once for all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_set ;
infile datalines truncover; 
input policy_number $100. ; 
want=prxchange('s/^0+|[^a-z\d]|MANUALENTRY|MANUAL|CEDEDMANUAL|MANUALCEDED//i',-1,strip(policy_number));
datalines; 
Q2 2017 Manual Entry 
000123400 
Manual 
Manual_fix15 
Manual_fix22Q1 
Manual All Data 
0098765401CededManual 
0098765401Manual 
0011223301 CededManual 
; 
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Oct 2022 11:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-array-to-remove-substrings-from-strings/m-p/839347#M331889</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-10-19T11:37:09Z</dc:date>
    </item>
  </channel>
</rss>

