<?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: Remove items from a list (string processing) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/408003#M99489</link>
    <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;that's nifty approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added a Ballot idea for a ReplaceW() function.&amp;nbsp; Not sure if it's really feasible, but I think it's what I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SASware-Ballot-Ideas/Create-ReplaceW-character-function-that-accepts-a-list-of-target/idi-p/408002#M2832" target="_blank"&gt;https://communities.sas.com/t5/SASware-Ballot-Ideas/Create-ReplaceW-character-function-that-accepts-a-list-of-target/idi-p/408002#M2832&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 27 Oct 2017 12:52:33 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2017-10-27T12:52:33Z</dc:date>
    <item>
      <title>Remove items from a list (string processing)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/407774#M99384</link>
      <description>&lt;P&gt;Often I have a list of items:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=Monday Tuesday Wednesday Thursday Friday Saturday;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And I want to remove n items from the list.&amp;nbsp; So suppose I want to remove Tuesday and Thursday.&amp;nbsp; You can do it with TRANWRD(), but if you are removing multiple words, it gets ugly fast if you nest them, and feels like it shouldn't need two function calls.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list2=%sysfunc(tranwrd(%sysfunc(tranwrd(&amp;amp;list,Tuesday,)),Thursday,));
%put &amp;amp;list2;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Sometimes when I have faced this I have written a little function-style macro to loop through the items of a list, checking to see if each item is in the list of items to remove.&amp;nbsp; Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro removeitem(list=,remove=);
  %local
    i
    item_i
    return
  ;
  %do i=1 %to %sysfunc(countw(&amp;amp;list,%str( )));
    %let item_i=%scan(&amp;amp;list,&amp;amp;i,%str( ));
    %if %sysfunc(findw(&amp;amp;remove,&amp;amp;item_i))=0 %then %let return=&amp;amp;return &amp;amp;item_i;
  %end;
  &amp;amp;return
%mend;

%let list3=%RemoveItem(list=&amp;amp;list,remove=Tuesday Thursday);
%put &amp;amp;list3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Above approaches work, but I never feel good about them.&amp;nbsp; I always assume there is a function I'm not thinking of, like a REMOVE() function, or one of the TRANSTRN/TRANWRD functions that would allow me to pass a list of strings to replace and a list of replacement strings, like TRANSLATE() allows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or maybe today's the day that I dive into regular expressions?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think of this as a macro problem, because that's usually where I encounter such lists, but if there is a nifty data step solution, happy to see that, especially if in can be macrified (macrocized?)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 18:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/407774#M99384</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-26T18:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: Remove items from a list (string processing)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/407783#M99391</link>
      <description>&lt;P&gt;Perhaps it's just a small piece of the puzzle, but you can simplify the list processing.&amp;nbsp; Instead of accumulating the items into a list, just generate the "non-removed" items one word at a time.&amp;nbsp; In this case:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token macrobound"&gt;%macro&lt;/SPAN&gt; removeitem&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;list&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;remove&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;SPAN class="token macroname"&gt;%local&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; i&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; item_i&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt; i&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%to&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;countw&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;list&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token macrostatement"&gt;%str&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; item_i&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token macroname"&gt;%scan&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;list&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token macrostatement"&gt;%str&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="token macrostatement"&gt;%if&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%sysfunc&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;findw&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;remove&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;item_i&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%then&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;item_i&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;SPAN class="token macrostatement"&gt;%end&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token macrobound"&gt;%mend&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; list3&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token macroname"&gt;%RemoveItem&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;list&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;list&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;remove&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;Tuesday Thursday&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token macrostatement"&gt;%put&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;list3&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2017 18:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/407783#M99391</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-10-26T18:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Remove items from a list (string processing)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/407940#M99459</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;:&lt;/P&gt;&lt;P&gt;One possibility is to use PRXCHANGE instead, e.g.:&lt;/P&gt;&lt;PRE&gt;%let list=Monday Tuesday Wednesday Thursday Friday Saturday;
%let list2=%sysfunc(prxchange(s/Tuesday|Thursday//,-1,&amp;amp;list));&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Oct 2017 08:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/407940#M99459</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-10-27T08:02:32Z</dc:date>
    </item>
    <item>
      <title>Re: Remove items from a list (string processing)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/408003#M99489</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;that's nifty approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I added a Ballot idea for a ReplaceW() function.&amp;nbsp; Not sure if it's really feasible, but I think it's what I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SASware-Ballot-Ideas/Create-ReplaceW-character-function-that-accepts-a-list-of-target/idi-p/408002#M2832" target="_blank"&gt;https://communities.sas.com/t5/SASware-Ballot-Ideas/Create-ReplaceW-character-function-that-accepts-a-list-of-target/idi-p/408002#M2832&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 12:52:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/408003#M99489</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-27T12:52:33Z</dc:date>
    </item>
    <item>
      <title>Re: Remove items from a list (string processing)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/408035#M99502</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;: While I agree that regular expressions are a lot more powerful and well worth learning, you can always simplify the task in a data step by using a do loop. Still uses multiple uses of the tranwrd function, but easier (for me at least) to read. e.g.:&lt;/P&gt;
&lt;PRE&gt;data have (drop=i);
  informat days $70.;
  length i $10;
  input days &amp;amp;;
  do i='Monday','Wednesday','Friday';
    days=tranwrd(days,i,'');
  end;
  cards;
Monday Tuesday Wednesday Wednesday Friday
Monday Tuesday Thursday Monday Sunday
;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 14:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/408035#M99502</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-10-27T14:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Remove items from a list (string processing)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/408114#M99532</link>
      <description>&lt;P&gt;Agree &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;,&amp;nbsp; the looping structure is much easier on the eyes.&amp;nbsp; Even more so in the macro setting.&amp;nbsp; I was prompted to start this thread because I had started with one replacement, then two, then three.&amp;nbsp; And ended up with to following atrocity in my code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let list=These SomeWordToRemove are SomeOtherWordToRemove the YetAnotherWordToRemove words to keep;
%put %sysfunc(tranwrd(%sysfunc(tranwrd(%sysfunc(tranwrd(&amp;amp;list,SomeWordToRemove,)),SomeOtherWordToRemove,)),YetAnotherWordToRemove,)) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I promised myself I would take the time to refactor it into a little function-style macro that does looping as you illustrated. : )&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2017 17:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-items-from-a-list-string-processing/m-p/408114#M99532</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-27T17:20:21Z</dc:date>
    </item>
  </channel>
</rss>

