<?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 How to replace a list of words with a space in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666725#M199541</link>
    <description>&lt;P&gt;I would like to clean company names. If the variable NAME contains INC, CORP, CLASS etc then I would like to remove them. Right now I use the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if find(name,' CORP ') then name_cleaned = tranwrd(name,' CORP ', ' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which works fine. However, if the name contains 2 words (e.g. ABC CORP CLASS A) that I want to delete, the above would only remove the word CORP and leave the word CLASS in the name. I can write another line of code such as&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if find(name,' CORP CLASS ') then name_cleaned = tranwrd(name,' CORP CLASS ', ' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works fine, but if I have 20 words which I need to remove, doing so is long and might not be ideal. So what function can I use to tell SAS that if NAME contains these words, remove them or replace them with a space?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 03 Jul 2020 03:02:36 GMT</pubDate>
    <dc:creator>somebody</dc:creator>
    <dc:date>2020-07-03T03:02:36Z</dc:date>
    <item>
      <title>How to replace a list of words with a space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666725#M199541</link>
      <description>&lt;P&gt;I would like to clean company names. If the variable NAME contains INC, CORP, CLASS etc then I would like to remove them. Right now I use the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if find(name,' CORP ') then name_cleaned = tranwrd(name,' CORP ', ' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which works fine. However, if the name contains 2 words (e.g. ABC CORP CLASS A) that I want to delete, the above would only remove the word CORP and leave the word CLASS in the name. I can write another line of code such as&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if find(name,' CORP CLASS ') then name_cleaned = tranwrd(name,' CORP CLASS ', ' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works fine, but if I have 20 words which I need to remove, doing so is long and might not be ideal. So what function can I use to tell SAS that if NAME contains these words, remove them or replace them with a space?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 03:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666725#M199541</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2020-07-03T03:02:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace a list of words with a space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666730#M199543</link>
      <description>&lt;P&gt;Regular Expressions are perfect for this type of work.&lt;/P&gt;
&lt;P&gt;This takes care of case too:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;NEWNAME=prxchange('s/CORP|CLASS|LTD|INC//i',-1,NAME);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 04:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666730#M199543</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-03T04:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace a list of words with a space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666731#M199544</link>
      <description>&lt;P&gt;Adapt next (updated) tested code to your needs:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list_to_clean = one two three four five;
%let n2c = %sysfunc(countw(&amp;amp;list_to_clean)); 
data _null_;
length list_to_clean $100;
     list2clean = "&amp;amp;list_to_clean";
     do i=1 to &amp;amp;n2c;
        list_to_clean = trim(list_to_clean) || ' ' || quote(trim(scan(list2clean,i)));
     end;
     call symput('List_to_Clean',trim(list_to_clean));
run;
%put XXX= &amp;amp;list_to_clean;

*%let list_to_clean = "one" "two" "three" "four" "five";


data have;
  length string $30;
  infile cards truncover;
  input string $30. ;
cards;
there is one tree
I have three sons
raise two fingers&lt;BR /&gt;Nothing to Clean
;
run;
data want;
 set have;
     array w2c {&amp;amp;n2c} $ (&amp;amp;list_to_clean);&lt;BR /&gt;     string1 = string;
     do i=1 to countw(string);
        w2check = scan(string,i);
        if w2check in w2c
        then string1 = tranwrd(string,strip(w2check),' ');  
     end;
     keep string string1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 05:23:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666731#M199544</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-03T05:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace a list of words with a space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666732#M199545</link>
      <description>&lt;P&gt;Note that to find words, the &lt;EM&gt;findw&lt;/EM&gt; function is better.&lt;/P&gt;
&lt;P&gt;In this case you need to add \b in the expression:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NAME = prxchange('s/\b(CLASS|LTD|INC)\b//i',-1,NAME);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might also want to remove double spaces.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;NAME = compbl(prxchange('s/\b(CLASS|LTD|INC)\b//i',-1,NAME));&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 04:29:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666732#M199545</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-07-03T04:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace a list of words with a space</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666734#M199547</link>
      <description>&lt;P&gt;Just one thing to add: when using tranwrd or translate it is not necessary to wrap calling those function in an if-then-statement. If those function don't find something to replace, they return the unchanged first parameter of the functions.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jul 2020 05:13:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-a-list-of-words-with-a-space/m-p/666734#M199547</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-07-03T05:13:14Z</dc:date>
    </item>
  </channel>
</rss>

