<?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: Counting the Values by Using Case When in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294749#M61514</link>
    <description>&lt;P&gt;There are couple of problem in the query&lt;/P&gt;&lt;P&gt;1. Use of aggregate function and use of distinct at the same time do not make any sense. Because aggregate function itself result one row per group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. The query need to group by &amp;nbsp;using Character column but it has used group by Numeric. The Group by should used on grouping variable which is Character in this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. Count function used in &lt;STRONG&gt;case when then&lt;/STRONG&gt; statement do not make any sense.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Aug 2016 10:42:03 GMT</pubDate>
    <dc:creator>RahulG</dc:creator>
    <dc:date>2016-08-29T10:42:03Z</dc:date>
    <item>
      <title>Counting the Values by Using Case When</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294744#M61509</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to ask a question to you. It is related to Proc Sql statement and Case When. I want to count the number "1" values in the"Numeric" variable also by grouping "Numeric". However, in the following example, the query brings two rows but actually, I expect just one row. Can somebody help me by checking the following sample, please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Have;
Length Character $ 8 Numeric 8;
Infile Datalines Missover;
Input Character	Numeric;
Datalines;
33 1
33 2
33 3
33 3
33 1
33 2
33 2
33 3
33 3
;
Run;

PROC SQL;
Create Table Want As
Select Distinct Character
,Case
When Numeric=1 Then Count(Numeric)
End As CountNumeric
From Have
Where Character='33'
Group By Numeric;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My desired output;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/4744i3C75739371F603B6/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="Desired.png" title="Desired.png" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2016 10:23:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294744#M61509</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2016-08-29T10:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the Values by Using Case When</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294746#M61511</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;--One way

PROC SQL;
Create Table Want As
Select Character
,count( numeric) as count_numeric
From Have
Where Character='33' and numeric =1
Group By character;
QUIT;


--Alternate way
PROC SQL;
Create Table Want2 As
Select Character
,sum( case when  numeric=1 then 1 end ) as count_numeric
From Have
Where Character='33' 
Group By character;
QUIT;



&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Aug 2016 10:33:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294746#M61511</guid>
      <dc:creator>RahulG</dc:creator>
      <dc:date>2016-08-29T10:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the Values by Using Case When</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294747#M61512</link>
      <description>&lt;P&gt;Thanks a lot, can you also give brief information about the reason? Why it was bring mising values in my query?&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2016 10:35:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294747#M61512</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2016-08-29T10:35:50Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the Values by Using Case When</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294749#M61514</link>
      <description>&lt;P&gt;There are couple of problem in the query&lt;/P&gt;&lt;P&gt;1. Use of aggregate function and use of distinct at the same time do not make any sense. Because aggregate function itself result one row per group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. The query need to group by &amp;nbsp;using Character column but it has used group by Numeric. The Group by should used on grouping variable which is Character in this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. Count function used in &lt;STRONG&gt;case when then&lt;/STRONG&gt; statement do not make any sense.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2016 10:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294749#M61514</guid>
      <dc:creator>RahulG</dc:creator>
      <dc:date>2016-08-29T10:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the Values by Using Case When</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294750#M61515</link>
      <description>&lt;PRE&gt;
use HAVING clause:

Data Have;
Length Character $ 8 Numeric 8;
Infile Datalines Missover;
Input Character	Numeric;
Datalines;
33 1
33 2
33 3
33 3
33 1
33 2
33 2
33 3
33 3
;
Run;

PROC SQL;
Select Character,Numeric,count(*) As CountNumeric
From Have
Group By Character,Numeric
having Character='33' and Numeric=1;
QUIT;


&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Aug 2016 10:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-Values-by-Using-Case-When/m-p/294750#M61515</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-29T10:57:30Z</dc:date>
    </item>
  </channel>
</rss>

