<?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 SAS - Multiple 'Or' filters based on table values in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612083#M18281</link>
    <description>&lt;P&gt;Hi - How do i use a reference table to create multiple 'or'&amp;nbsp; filters For example, this&amp;nbsp; code i'm looking for would read the below reference table and create a filter with the below criteria. I need an automated/dynamic way to create this filter because the&lt;STRONG&gt; code itself cannot be changed,&lt;/STRONG&gt; but new records can be freely inserted and deleted from the reference table. Ideally I would like to use this reference table and store this filter criteria inside macro variables, and then apply these macro variable filters to a different table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;if ((state='NJ' and ID_Number=1) or (state='NY' and ID_Number=4));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Reference Table" style="width: 188px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34799iE304A21B0802FB4A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Reference Table.JPG" alt="Reference Table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Reference Table&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Dec 2019 17:15:02 GMT</pubDate>
    <dc:creator>dad316</dc:creator>
    <dc:date>2019-12-16T17:15:02Z</dc:date>
    <item>
      <title>SAS - Multiple 'Or' filters based on table values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612083#M18281</link>
      <description>&lt;P&gt;Hi - How do i use a reference table to create multiple 'or'&amp;nbsp; filters For example, this&amp;nbsp; code i'm looking for would read the below reference table and create a filter with the below criteria. I need an automated/dynamic way to create this filter because the&lt;STRONG&gt; code itself cannot be changed,&lt;/STRONG&gt; but new records can be freely inserted and deleted from the reference table. Ideally I would like to use this reference table and store this filter criteria inside macro variables, and then apply these macro variable filters to a different table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;if ((state='NJ' and ID_Number=1) or (state='NY' and ID_Number=4));&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Reference Table" style="width: 188px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34799iE304A21B0802FB4A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Reference Table.JPG" alt="Reference Table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Reference Table&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 17:15:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612083#M18281</guid>
      <dc:creator>dad316</dc:creator>
      <dc:date>2019-12-16T17:15:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - Multiple 'Or' filters based on table values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612086#M18282</link>
      <description>&lt;P&gt;If you already have the reference data in a data set it is likely to be easier to use that to filter the data with Proc Sql. No need for any macro variables for this task unless you like to cause headaches.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A very brief example:&lt;/P&gt;
&lt;PRE&gt;data work.bigdata;
   input state $ Id_number var1;
datalines;
MA 123 45
NJ 1   27
NJ 2   15
NJ 1   18
NH 34  1
NY 4   12
NY 4   3
NY 6   8
;

data work.reftable;
   input state $ Id_number ;
datalines;
NJ  1
NY  4
;

proc sql;
   create table work.want as
   select b.* 
   from work.reftable as a
        left join
        work.bigdata as b
        on a.state=b.state
        and a.id_number =b.id_number
   ;
quit;
&lt;/PRE&gt;
&lt;P&gt;The aliases A and B are short hand to reference the data sets and prefix variables to indicate which data set value you want to use.&lt;/P&gt;
&lt;P&gt;The Left Join says "find all of the values from A with a match in B" using the criteria in the ON clause, in this case the state and Id_number variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 17:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612086#M18282</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-16T17:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - Multiple 'Or' filters based on table values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612093#M18283</link>
      <description>A join is a better filter here than dynamically creating multiple filters. The only exception to this would be if you were using explicit pass through code.</description>
      <pubDate>Mon, 16 Dec 2019 17:37:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612093#M18283</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-12-16T17:37:29Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - Multiple 'Or' filters based on table values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612173#M18289</link>
      <description>&lt;P&gt;The issue here is that i don't actually need to filter the entire dataset and only keep records that meet the criteria of the source table. The records in the reference table will need to be flagged so i can subsequently then include them or exclude them. The reference table will just flag records that are special cases.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry i should've specified this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also need to apply the filter in a data step here (although i have the flexibility to create macro variables inside proc sql)&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:51:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612173#M18289</guid>
      <dc:creator>dad316</dc:creator>
      <dc:date>2019-12-16T19:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - Multiple 'Or' filters based on table values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612175#M18290</link>
      <description>Still better to merge, just merge with a full join instead and then create a flag if the merge is successful. &lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;merge have filter (in=a);&lt;BR /&gt;by state id_number;&lt;BR /&gt;if a then flag=1; else flag=0;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Now you have a flag variable that you can use to decide what to do later on.</description>
      <pubDate>Mon, 16 Dec 2019 19:24:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612175#M18290</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-12-16T19:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS - Multiple 'Or' filters based on table values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612235#M18294</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/193412"&gt;@dad316&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The issue here is that i don't actually need to filter the entire dataset and only keep records that meet the criteria of the source table. The records in the reference table will need to be flagged so i can subsequently then include them or exclude them. The reference table will just flag records that are special cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry i should've specified this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also need to apply the filter in a data step here (although i have the flexibility to create macro variables inside proc sql)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You posted example code:&lt;/P&gt;
&lt;PRE&gt;if ((state='NJ' and ID_Number=1) or (state='NY' and ID_Number=4));
&lt;/PRE&gt;
&lt;P&gt;Does indeed subset your data. An IF without then as you show removes any record that does not have a "true" result for the comparison from the resulting data.&lt;/P&gt;
&lt;P&gt;So I think you need to shows us a more complete example of what you are doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can merge data and flag things conditionally:&lt;/P&gt;
&lt;PRE&gt;proc sort data=work.bigdata;
   by state id_number;
run;
proc sort data=work.reftable;
   by state id_number;
run;

data work.combined;
   merge work.bigdata
         work.reftable (in=inref);
   by state id_number;
   if inref then Reftable='Yes';
run;&lt;/PRE&gt;
&lt;P&gt;Not that I would personally be likely to use a variable like Reftable as character. But that shows which ones matched. Likely you could use the INREF (which is true when the current record matches values from the reftable data). The data set option IN= creates a temporary variable with values of 1/0 (true or false) indicating membership of that data set for the current record. Each data set could use the IN= option to create more information with all of the combinations of sets (3 in this case). A record could come from bigdata but not reftable, from both, or from reftable but not bigdata.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 22:02:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Multiple-Or-filters-based-on-table-values/m-p/612235#M18294</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-16T22:02:33Z</dc:date>
    </item>
  </channel>
</rss>

