<?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 Proc SQL Subquery in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617947#M181170</link>
    <description>&lt;P&gt;The following code first determines how many Brand Name drugs have just one HCPCS Code and then uses that list to pull Code, Brand Name and Generic name for those drugs from the same data source.&amp;nbsp; &amp;nbsp;However, is there a way to combine this into one query, perhaps with the first query as a subquery in the second?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;Create Table Step2a as select Brand_name, count(*) as Cnt&lt;BR /&gt;from Spend_util&lt;BR /&gt;Group by Brand_Name&lt;BR /&gt;having Count(distinct HCPCS) = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Create Table Step2b as select a.HCPCS label='HCPCS Code', a.Brand_name, a.Generic_Name&lt;BR /&gt;from Spend_util as a, Step2a as b&lt;BR /&gt;where a.Brand_name=b.Brand_name;&lt;BR /&gt;quit;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jan 2020 22:10:04 GMT</pubDate>
    <dc:creator>Batman</dc:creator>
    <dc:date>2020-01-16T22:10:04Z</dc:date>
    <item>
      <title>Proc SQL Subquery</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617947#M181170</link>
      <description>&lt;P&gt;The following code first determines how many Brand Name drugs have just one HCPCS Code and then uses that list to pull Code, Brand Name and Generic name for those drugs from the same data source.&amp;nbsp; &amp;nbsp;However, is there a way to combine this into one query, perhaps with the first query as a subquery in the second?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;Create Table Step2a as select Brand_name, count(*) as Cnt&lt;BR /&gt;from Spend_util&lt;BR /&gt;Group by Brand_Name&lt;BR /&gt;having Count(distinct HCPCS) = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Create Table Step2b as select a.HCPCS label='HCPCS Code', a.Brand_name, a.Generic_Name&lt;BR /&gt;from Spend_util as a, Step2a as b&lt;BR /&gt;where a.Brand_name=b.Brand_name;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 22:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617947#M181170</guid>
      <dc:creator>Batman</dc:creator>
      <dc:date>2020-01-16T22:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Subquery</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617950#M181172</link>
      <description>&lt;P&gt;Is the only thing wrong with your Step2a dataset that it has the Cnt variable and your label's aren't set yet?&lt;/P&gt;
&lt;P&gt;If so just add your labels in that step, your additional columns and add their grouping after the brand name.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also get rid of your count(*) as Cnt from the select; it doesn't need to be included as the logic is being applied with your having clause.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Like this, just compare results make sure they match. If not you might have to play with your group by.&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 HCPCS label='HCPCS Code', Brand_name, Generic_Name
from Spend_util
group by Brand_Name, HCPCS, Generic_Name
having Count(distinct HCPCS) = 1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Jan 2020 22:36:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617950#M181172</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2020-01-16T22:36:30Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Subquery</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617955#M181177</link>
      <description>&lt;P&gt;Thanks, but I don't think that would work, since we want to know which Brand Names have more than one HCPCS Code.&amp;nbsp; &amp;nbsp;If we group by Brand Name, Generic Name and HCPCS, wouldn't the count always be 1?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 22:41:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617955#M181177</guid>
      <dc:creator>Batman</dc:creator>
      <dc:date>2020-01-16T22:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Subquery</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617964#M181184</link>
      <description>&lt;P&gt;I suppose I don't understand how your step 2 is applying that logic as I don't see it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want counts &amp;gt; 1 change it from = to &amp;gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 23:06:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617964#M181184</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2020-01-16T23:06:01Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Subquery</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617993#M181210</link>
      <description>Are you assuming it won't work, or did you test it and did it not work?</description>
      <pubDate>Fri, 17 Jan 2020 02:42:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Subquery/m-p/617993#M181210</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-17T02:42:30Z</dc:date>
    </item>
  </channel>
</rss>

