<?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: Filter dataset &amp;amp; variable by a value list of a macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664947#M198727</link>
    <description>&lt;P&gt;BTW Welcome to the communities!&lt;/P&gt;</description>
    <pubDate>Thu, 25 Jun 2020 10:16:14 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-06-25T10:16:14Z</dc:date>
    <item>
      <title>Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664938#M198721</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;i need some assistance to resolve the following problem, please. I want to save a value list in a macro variable (&amp;amp;teams), which then is used for a comparison with the values of a variable (mapIDname) from a different dataset. The final dataset (a2) should remain only information regarding the values of the macro variable. Hope the code makes the aim of this effort clearer. Can anyone help to work my code? I guess something is missing. Thank you for any suggestion! Chris.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 10:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664938#M198721</guid>
      <dc:creator>coj</dc:creator>
      <dc:date>2020-06-25T10:01:19Z</dc:date>
    </item>
    <item>
      <title>Betreff: Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664942#M198723</link>
      <description>* Save value list in macro variable ;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;select distinct team into :teams&lt;BR /&gt;from sashelp.baseball;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;teams;&lt;BR /&gt;&lt;BR /&gt;* List of interest ;&lt;BR /&gt;&lt;BR /&gt;data a1; set sashelp.gcstate; run;&lt;BR /&gt;&lt;BR /&gt;* Filter list of interest by &amp;amp;teams;&lt;BR /&gt;&lt;BR /&gt;data a2(where=(select=1)); set a1;&lt;BR /&gt;select=0;&lt;BR /&gt;if mapIDname IN ('&amp;amp;teams') then select=1; /* no working */&lt;BR /&gt;run;</description>
      <pubDate>Thu, 25 Jun 2020 10:11:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664942#M198723</guid>
      <dc:creator>coj</dc:creator>
      <dc:date>2020-06-25T10:11:18Z</dc:date>
    </item>
    <item>
      <title>Betreff: Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664946#M198726</link>
      <description>&lt;P&gt;I will try to lead you to find the answer yourself.&lt;/P&gt;
&lt;P&gt;How would you write your data step if you wanted to filter for teams Houston and Cleveland, without any macro reference?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2(where=(select=1));
set a1;
select=0;
if mapIDname IN (/* what would you put in here manually? */) then select=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jun 2020 10:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664946#M198726</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-25T10:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664947#M198727</link>
      <description>&lt;P&gt;BTW Welcome to the communities!&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 10:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664947#M198727</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-25T10:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664949#M198728</link>
      <description>Thank you Kurt for the rapid reaction. Your hint suggests to use Tokens within parenthesis. However, the &amp;amp;team macro variable is a variable and contains different numbers of values. This step will be repeated more than 1000 times with different value lists. Thus, i need a solution with macro variable. Any idea Kurt?</description>
      <pubDate>Thu, 25 Jun 2020 10:23:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664949#M198728</guid>
      <dc:creator>coj</dc:creator>
      <dc:date>2020-06-25T10:23:44Z</dc:date>
    </item>
    <item>
      <title>Betreff: Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664954#M198731</link>
      <description>&lt;P&gt;You need to get the values for your IN clause quoted, so that you will have a list like 'team1','team2','team3', not&amp;nbsp;'team1,team2,team3', and you should use SEPARATED BY to get the list of values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Save value list in macro variable ;

proc sql noprint;
select distinct quote(trim(team),"'") into :teams separated by ','
from sashelp.baseball;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which you can then use in your IN clause:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2(where=(select=1)); 
set a1;
select=mapIDname IN (&amp;amp;teams);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why have the SELECT variable? It is always 1 on the output, anyway. So you should simplify to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2;
  set a1;
  where mapIDname in(&amp;amp;teams);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The query will also run faster if you put the WHERE clause on the input table, as shown.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 11:07:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664954#M198731</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-25T11:07:08Z</dc:date>
    </item>
    <item>
      <title>Re: Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664955#M198732</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/335292"&gt;@coj&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you Kurt for the rapid reaction. Your hint suggests to use Tokens within parenthesis. However, the &amp;amp;team macro variable is a variable and contains different numbers of values. This step will be repeated more than 1000 times with different value lists. Thus, i need a solution with macro variable. Any idea Kurt?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think Kurt wants you to create a working example with two "tokens" or values, not thousands. This is almost a mandatory first step if you are going to get macros or macro variables to work properly. If you can't get code to work without macros and without macro variables, then you will not be able to get it to work with macros and with macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, please provide the answer to a simple question, how would you do this for 2 values, without macros and without macro variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if mapIDname IN (/* what would you put in here manually? */) then select=1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jun 2020 11:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664955#M198732</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-25T11:07:20Z</dc:date>
    </item>
    <item>
      <title>Re: Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664958#M198733</link>
      <description>&lt;P&gt;Step 1: start with a working example for one instance, in this case, I take my Houston, Cleveland example.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2(where=(select=1));
set a1;
select=0;
if mapIDname IN ('Houston','Cleveland') then select=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since 0 and 1 are the boolean true and false values in SAS, and you only want true, we can simplify this a lot:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2;
set a1;
if mapIDname IN ('Houston','Cleveland');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But we see that we can't use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IN ("Houston,Cleveland");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which is what your code would have created if you had used double quotes (macro variables are not resolved when enclosed in single quotes).&lt;/P&gt;
&lt;P&gt;So we need to set the quotes when we create the macro variables in the first place:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select distinct quote(trim(team)) into :teams separated by ','
from sashelp.baseball;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The resulting macro variable can now be used in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a2;
set a1;
if mapIDname IN (&amp;amp;teams);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you should also take a step back and look at your process from a certain mental distance. You say you have to repeat that 1000 times; it might be possible (and better overall) to solve that with a single join that creates a grouped output dataset, with each group representing one of your selections.&lt;/P&gt;
&lt;P&gt;To see if this can be done, I would like to see examples of your real datasets. Please post i usable fashion (data steps with datalines, or similar, so I can recreate your datasets with copy/paste and submit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;EM&gt;Edit: simplified the SQL code, as done by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 11:19:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664958#M198733</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-25T11:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Filter dataset &amp; variable by a value list of a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664970#M198736</link>
      <description>Thank you Kurt! My missing information was this: "double quotes (macro variables are not resolved when enclosed in single quotes)". Now it is working within my environment due to the [quote(trim(team)) into :teams separated by ','] statement! Thanx a lot! Chris.</description>
      <pubDate>Thu, 25 Jun 2020 11:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filter-dataset-amp-variable-by-a-value-list-of-a-macro-variable/m-p/664970#M198736</guid>
      <dc:creator>coj</dc:creator>
      <dc:date>2020-06-25T11:50:57Z</dc:date>
    </item>
  </channel>
</rss>

