<?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 a string from a macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332246#M74764</link>
    <description>&lt;P&gt;You probably need to consider the possibility that the name of the variable you want to remove is a substring of another variable in your list. You could add some spaces to the first two arguments to make it match words. Also watch out for case of the variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;10   %let varlist=V1v3 V2 V3 ;
11   %let var=v3 ;
12   %let want=%sysfunc(tranwrd(%str( %upcase(&amp;amp;varlist) ),%str( %upcase(&amp;amp;var) ),%str( )));
13   %put WANT=|&amp;amp;want|;
WANT=|V1V3 V2|&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if you want to the second parameter to also be a list you will need a more complicated solution. &amp;nbsp;You could do it with %DO looip or possible using regular expressions.&lt;/P&gt;</description>
    <pubDate>Mon, 13 Feb 2017 16:46:03 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-02-13T16:46:03Z</dc:date>
    <item>
      <title>Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332233#M74757</link>
      <description>&lt;P&gt;if I define 2 lists of variables as :&lt;/P&gt;&lt;P&gt;%let varlist1 = v1 v2 v3&lt;/P&gt;&lt;P&gt;%let varlist2 = v3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How could I generate a new variable list consisting of v1 and v2 by subtracting varlist2 from varlist1? The following command does not seem to give what I want.&lt;/P&gt;&lt;P&gt;%let varlist3 = tranwrd(&amp;amp;varlist1., &amp;amp;varlist2., '').&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 16:28:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332233#M74757</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2017-02-13T16:28:38Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332238#M74760</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;Yout code works, but you must call tranwrd inside a %sysfunc macro function to use it outside a data step, and also omit the quotes around the empty value, as they are considered part of the value in macro language.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let varlist3 = %sysfunc(tranwrd(&amp;amp;varlist1., &amp;amp;varlist2., %str()));&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 16:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332238#M74760</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2017-02-13T16:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332239#M74761</link>
      <description>It works. Thank you!</description>
      <pubDate>Mon, 13 Feb 2017 16:38:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332239#M74761</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2017-02-13T16:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332240#M74762</link>
      <description>&lt;P&gt;- Note that it only works with 1 value in varlist2, as the tranword function takes the list as a single string to search for. It is more complicated with several values. Do you need that?&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 16:39:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332240#M74762</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2017-02-13T16:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332246#M74764</link>
      <description>&lt;P&gt;You probably need to consider the possibility that the name of the variable you want to remove is a substring of another variable in your list. You could add some spaces to the first two arguments to make it match words. Also watch out for case of the variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;10   %let varlist=V1v3 V2 V3 ;
11   %let var=v3 ;
12   %let want=%sysfunc(tranwrd(%str( %upcase(&amp;amp;varlist) ),%str( %upcase(&amp;amp;var) ),%str( )));
13   %put WANT=|&amp;amp;want|;
WANT=|V1V3 V2|&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if you want to the second parameter to also be a list you will need a more complicated solution. &amp;nbsp;You could do it with %DO looip or possible using regular expressions.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 16:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332246#M74764</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-02-13T16:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332247#M74765</link>
      <description>Yes I would like to know that. What if varlist2 is defined as&lt;BR /&gt;%varlist2 = v1 v2;</description>
      <pubDate>Mon, 13 Feb 2017 16:49:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332247#M74765</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2017-02-13T16:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332253#M74768</link>
      <description>&lt;P&gt;As you all suggested, this solution doesn't work if varlist 2 contains multiple stirngs separated by spaces. In fact, none of the components would be removed from varlist1. Can you please give a solution to that scenario?&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 16:58:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332253#M74768</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2017-02-13T16:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332258#M74769</link>
      <description>&lt;P&gt;It comes here.. You need a macro to remove the values in varlist2 one at a time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let varlist1 = V1 V2 V3 V4 V5;&lt;BR /&gt;%let varlist2 = V3 V1 ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro m;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%global varlist3;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%let varlist3 = &amp;amp;varlist1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%do i = 1 %to %sysfunc(countw(&amp;amp;varlist2));&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; %let varlist3 = %sysfunc(tranwrd(&amp;amp;varlist3., %scan(&amp;amp;varlist2.,&amp;amp;i,%str( )), %str()));&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%let varlist3 = %sysfunc(compbl(&amp;amp;varlist3));&lt;BR /&gt;%mend;&lt;BR /&gt;%m;&lt;BR /&gt;%put &amp;amp;varlist3;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Feb 2017 17:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332258#M74769</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2017-02-13T17:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332263#M74770</link>
      <description>&lt;P&gt;If you need to do it totally in macro code then you probably want a loop. &amp;nbsp;For example you could make a new macro "function".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro subset(list1,list2);
%local i word result;
%do i=1 %to %sysfunc(countw(&amp;amp;list1));
   %let word=%scan(&amp;amp;list1,&amp;amp;i);
   %if not %index(%str( %upcase(&amp;amp;list2) ),%str( %upcase(&amp;amp;word) )) %then
     %let result=&amp;amp;result &amp;amp;word 
  ;
%end;
&amp;amp;result.
%mend subset;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So then you can use it this way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;38   %let want=%subset(V1 v2 v3 v4 v5 V3v2,V3 v2);
39   %put WANT=|&amp;amp;want|;
WANT=|V1 v4 v5 V3v2|&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Feb 2017 17:21:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332263#M74770</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-02-13T17:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332320#M74782</link>
      <description>Thanks so much. I got it to work.</description>
      <pubDate>Mon, 13 Feb 2017 20:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332320#M74782</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2017-02-13T20:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a string from a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332463#M74837</link>
      <description>&lt;PRE&gt;
%let varlist1 = V1 V2 V3 V4 V5;
%let varlist2 = V3 V1;

%let pid=\b%sysfunc(prxchange(s/\s+/\b|\b/,-1,&amp;amp;varlist2))\b;
%let want=%sysfunc(prxchange(s/&amp;amp;pid//i,-1,&amp;amp;varlist1));

%put &amp;amp;want;


&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Feb 2017 03:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-string-from-a-macro-variable/m-p/332463#M74837</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-02-14T03:28:52Z</dc:date>
    </item>
  </channel>
</rss>

