<?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: conditional count in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/conditional-count-in-proc-sql/m-p/168691#M264063</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since SAS evaluates boolean expressions to 1/0 you can just change the second COUNT() to SUM().&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 Feb 2014 14:44:59 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2014-02-10T14:44:59Z</dc:date>
    <item>
      <title>conditional count in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-count-in-proc-sql/m-p/168689#M264061</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear All:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Could you please tell me how to count a variable only if certain criteria is met in the proc sql syntax.&amp;nbsp; For example, I want to count both people and Hispanic people.&amp;nbsp; So I coded the following way, but the calculated p_count and chm_count is the same.&amp;nbsp; Also, if I want to count rows in which certain variable called sex is F, could I go for count(sex = "F")?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My old but stupid way is to use proc sql two time with the where statement and then join these two tables.&amp;nbsp; That is not cool.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-size: 12.0pt; font-family: Consolas;"&gt;proc&lt;/STRONG&gt; &lt;STRONG style="color: navy; background: white; font-size: 12.0pt; font-family: Consolas;"&gt;sql&lt;/STRONG&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;create&lt;/SPAN&gt; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;table&lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt; ipums2 &lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;as&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;select&lt;/SPAN&gt; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;distinct&lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt; year, statefip, county, count(pid) &lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt; p_count, count(race=&lt;/SPAN&gt;&lt;STRONG style="color: teal; background: white; font-size: 12.0pt; font-family: Consolas;"&gt;4&lt;/STRONG&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt;) &lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt; hs_count&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;from&lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt; ipums&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;group&lt;/SPAN&gt; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;by&lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt; year, statefip, county&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;order&lt;/SPAN&gt; &lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: blue; background: white;"&gt;by&lt;/SPAN&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt; year, statefip, county;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-size: 12.0pt; font-family: Consolas;"&gt;quit&lt;/STRONG&gt;&lt;SPAN style="font-size: 12.0pt; font-family: Consolas; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Feb 2014 08:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-count-in-proc-sql/m-p/168689#M264061</guid>
      <dc:creator>caveman529</dc:creator>
      <dc:date>2014-02-10T08:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: conditional count in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-count-in-proc-sql/m-p/168690#M264062</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't know what your final result should be (in what form). In many cases, just adding your column to the group by clause is sufficient. If you want specific grouping, you can hold these in control tables (and apply them using SAS formats or SQL join). Adding specific criteria as a basic technique can lead to hard coded programs which lead higher maintenance costs.&lt;/P&gt;&lt;P&gt;Having that said, you could combine case with aggregation functions:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sum(case race when 4 then 1 else 0 end) as hs_count&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Feb 2014 10:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-count-in-proc-sql/m-p/168690#M264062</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2014-02-10T10:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: conditional count in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-count-in-proc-sql/m-p/168691#M264063</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since SAS evaluates boolean expressions to 1/0 you can just change the second COUNT() to SUM().&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Feb 2014 14:44:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-count-in-proc-sql/m-p/168691#M264063</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2014-02-10T14:44:59Z</dc:date>
    </item>
  </channel>
</rss>

