<?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 Selecting Observations from One Table Only When They Match Criteria in Another Table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592041#M169709</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below I create two tables, with the second table a subset of the first. What I'd like is to retain only the observations from the first table where the MemberCTG and HCPCS on a given observation in the first table matches the MemberCTG and HCPCS on an observation on the second table.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My thought was to somehow pass the MemberCTGs from the second table into a macro list and filter the first table on those, then do the same thing with the HCPCs from the second table and filter the first table on that again. But that seems like it ends up being a far too complicated solution for the problem I'm trying to solve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE cb.LONGTERMOX09252019_Filter1 AS 
   SELECT t1.ClaimID, 
          t1.LineNum, 
          t1.MemberID, 
          t1.MemberCTG, 
          t1.date, 
          t1.HCPCS, 
          t1.billed, 
          t1.allowed, 
          t1.paid, 
          t1.MR_Cases_Admits, 
          t1.MR_Units_Days, 
          t1.MR_Procs
      FROM cb.LongTermOx09252019 t1
      ORDER BY t1.MemberCTG,
               t1.HCPCS,
               t1.date;
QUIT;

data cb.LONGTERMOX09252019_Filter2 (keep=MemberCTG HCPCS Count);
  set cb.LONGTERMOX09252019_Filter1;
  by MemberCTG HCPCS;
  if first.HCPCS then count=0;
  count+1;
  if last.HCPCS and count &amp;gt; 35 then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 26 Sep 2019 22:00:25 GMT</pubDate>
    <dc:creator>acemanhattan</dc:creator>
    <dc:date>2019-09-26T22:00:25Z</dc:date>
    <item>
      <title>Selecting Observations from One Table Only When They Match Criteria in Another Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592041#M169709</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below I create two tables, with the second table a subset of the first. What I'd like is to retain only the observations from the first table where the MemberCTG and HCPCS on a given observation in the first table matches the MemberCTG and HCPCS on an observation on the second table.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My thought was to somehow pass the MemberCTGs from the second table into a macro list and filter the first table on those, then do the same thing with the HCPCs from the second table and filter the first table on that again. But that seems like it ends up being a far too complicated solution for the problem I'm trying to solve.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE cb.LONGTERMOX09252019_Filter1 AS 
   SELECT t1.ClaimID, 
          t1.LineNum, 
          t1.MemberID, 
          t1.MemberCTG, 
          t1.date, 
          t1.HCPCS, 
          t1.billed, 
          t1.allowed, 
          t1.paid, 
          t1.MR_Cases_Admits, 
          t1.MR_Units_Days, 
          t1.MR_Procs
      FROM cb.LongTermOx09252019 t1
      ORDER BY t1.MemberCTG,
               t1.HCPCS,
               t1.date;
QUIT;

data cb.LONGTERMOX09252019_Filter2 (keep=MemberCTG HCPCS Count);
  set cb.LONGTERMOX09252019_Filter1;
  by MemberCTG HCPCS;
  if first.HCPCS then count=0;
  count+1;
  if last.HCPCS and count &amp;gt; 35 then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Sep 2019 22:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592041#M169709</guid>
      <dc:creator>acemanhattan</dc:creator>
      <dc:date>2019-09-26T22:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Observations from One Table Only When They Match Criteria in Another Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592050#M169712</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select TABLE1.* 
  from TABLE1 
      ,(select unique MemberCTG, HCPCS from TABLE2)
  where TABLE1.MemberCTG = TABLE2.MemberCTG
    and TABLE1.HCPCS     = TABLE2.HCPCS;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 22:51:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592050#M169712</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-09-26T22:51:35Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Observations from One Table Only When They Match Criteria in Another Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592068#M169719</link>
      <description>&lt;P&gt;The output isn't quite what I'm looking for in the sense that it's not a data set I can export and use for modeling.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2019 03:01:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592068#M169719</guid>
      <dc:creator>acemanhattan</dc:creator>
      <dc:date>2019-09-27T03:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Observations from One Table Only When They Match Criteria in Another Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592070#M169721</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50108"&gt;@acemanhattan&lt;/a&gt;&amp;nbsp;&amp;nbsp;I wish there was a SQL keyword to CREATE tables. It would be so nice to be able to CREATE tables. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2019 03:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592070#M169721</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-09-27T03:13:17Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Observations from One Table Only When They Match Criteria in Another Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592071#M169722</link>
      <description>&lt;P&gt;Here is another way of doing it, although it takes two passes at the second table&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as(
select t1.* 
from cb.LongTermOx09252019 t1 
where t1.MemberCTG in (select distinct MemberCTG from cb.LONGTERMOX09252019_Filter2) and 
t1.HCPCS in (Select distinct HCPCS from cb.LONGTERMOX09252019_Filter2)
);
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2019 03:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592071#M169722</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2019-09-27T03:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Observations from One Table Only When They Match Criteria in Another Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592088#M169729</link>
      <description>An in clause takes a lot longer to run than a join.&lt;BR /&gt;</description>
      <pubDate>Fri, 27 Sep 2019 05:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592088#M169729</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-09-27T05:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Observations from One Table Only When They Match Criteria in Another Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592089#M169730</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/50108"&gt;@acemanhattan&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking at the code you've posted I believe you could get the desired result directly via a group by/having clause&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE cb.want AS 
   SELECT t1.ClaimID, 
          t1.LineNum, 
          t1.MemberID, 
          t1.MemberCTG, 
          t1.date, 
          t1.HCPCS, 
          t1.billed, 
          t1.allowed, 
          t1.paid, 
          t1.MR_Cases_Admits, 
          t1.MR_Units_Days, 
          t1.MR_Procs
      FROM cb.LongTermOx09252019 t1
      group BY t1.MemberCTG,
               t1.HCPCS
      having count(*) &amp;gt; 35
      ORDER BY t1.MemberCTG,
               t1.HCPCS,
               t1.date;
      ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Sep 2019 05:30:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592089#M169730</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-09-27T05:30:52Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting Observations from One Table Only When They Match Criteria in Another Table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592228#M169778</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;this worked perfectly.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2019 16:31:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-Observations-from-One-Table-Only-When-They-Match/m-p/592228#M169778</guid>
      <dc:creator>acemanhattan</dc:creator>
      <dc:date>2019-09-27T16:31:44Z</dc:date>
    </item>
  </channel>
</rss>

