<?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 distinct within various groups in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106307#M29621</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yeah. Tom 's code is absolutely right and fast. I just want to rewrite it by using sub-query which is also a powerful tool of SQL as its cartesian product . Don't be offended , TOM.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data have ;

&amp;nbsp; input (a b c) ($) @@;

cards;
AA 1 A1 AA 1 A2 AA 1 A3 AA 2 A4 AA 2 A3 BB 1 B1 BB 1 B2 CC 1 .
;

proc sql ;
create table want as
select distinct a,b,count(distinct c) as cnt1,(select count(distinct c) from have where a=x.a ) as cnt2
&amp;nbsp; from have&amp;nbsp; as x
&amp;nbsp;&amp;nbsp; group by a,b
;
quit;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 26 Oct 2012 02:33:47 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2012-10-26T02:33:47Z</dc:date>
    <item>
      <title>Counting distinct within various groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106304#M29618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope someone can help ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have the following hypothetical dataset:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="251" style="border: 1px solid rgb(0, 0, 0); width: 310px; height: 223px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;MainAccount&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;ClientNum&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;AlternativeAccount&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;AA&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;A1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;AA&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;A2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;AA&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;A3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;AA&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;A4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;AA&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;A3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;BB&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;B1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;BB&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;B2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;CC&lt;/TD&gt;&lt;TD style="padding: 2px; text-align: center;"&gt;1&lt;/TD&gt;&lt;TD style="text-align: center;"&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You will note that main account AA has two clients linked to it and accounts BB and CC have one client linked to each. Account CC has no alternative accounts&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In account AA, each of the clients have different alternative accounts, but they share 1 (A3)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to count how many alternative accounts each client has and how many alternatve account each main account has (without double counting A3 for the first account). The problem is counting only the distinct alternative account numbers when looking at a main account level. I tried using a count(case ...) statement but it will always double count the A3 account and i cannot use a nested statement to say only count it once when is is not distinct ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My ideal final dataset needs to like like follow: (that is: &lt;STRONG&gt;NumAccount_AltAccounts&lt;/STRONG&gt; for Account AA must show 4 not 5)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="126" style="border: 1px solid rgb(0, 0, 0); width: 493px; height: 71px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;MainAccount&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;ClientNum&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;NumClient_AltAccounts&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;NumAccount_AltAccounts&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;AA&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;AA&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;BB&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;CC&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Oct 2012 09:50:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106304#M29618</guid>
      <dc:creator>Maheshvaran</dc:creator>
      <dc:date>2012-10-25T09:50:43Z</dc:date>
    </item>
    <item>
      <title>Re: Counting distinct within various groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106305#M29619</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Key idea is to use COUNT(DISTINCT varname) to get the counts. &lt;/P&gt;&lt;P&gt;Then you can create two queries that group by different variables and merge the results to get your final table.&lt;/P&gt;&lt;P&gt;I shortened the variable names to make it easier to type.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data have ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; input (a b c) ($) @@;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;cards;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;AA 1 A1 AA 1 A2 AA 1 A3 AA 2 A4 AA 2 A3 BB 1 B1 BB 1 B2 CC 1 .&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;proc sql ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;create table want as&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; select x.*,y.cnt2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; from&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp; (select distinct a,b,count(distinct c) as cnt1 from have group by a,b) x&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; ,(select distinct a,count(distinct c) as cnt2 from have group by a) y&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; where x.a = y.a &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; order by 1,2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data _null_;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; set want;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; put (_ALL_) (=);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;a=AA b=1 cnt1=3 cnt2=4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;a=AA b=2 cnt1=2 cnt2=4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;a=BB b=1 cnt1=2 cnt2=2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;a=CC b=1 cnt1=0 cnt2=0&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Oct 2012 11:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106305#M29619</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-10-25T11:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: Counting distinct within various groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106306#M29620</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tom's SQL is no doubt the most pithy approach, here is a data step using 2XDOW:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;infile cards truncover;&lt;/P&gt;&lt;P&gt;input (MainAccount ClientNum AlternativeAccount) (:$);&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;AA 1 A1&lt;/P&gt;&lt;P&gt;AA 1 A2&lt;/P&gt;&lt;P&gt;AA 1 A3&lt;/P&gt;&lt;P&gt;AA 2 A4&lt;/P&gt;&lt;P&gt;AA 2 A3&lt;/P&gt;&lt;P&gt;BB 1 B1&lt;/P&gt;&lt;P&gt;BB 1 B2&lt;/P&gt;&lt;P&gt;CC 1&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;/P&gt;&lt;P&gt;by MainAccount ClientNum AlternativeAccount;run;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array aa(100) $2. _temporary_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do _n_=1 by 1 until (last.MainAccount);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;by MainAccount;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if AlternativeAccount not in aa then aa(_n_)=AlternativeAccount;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; num_client=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; num_account=100-cmiss(of aa(*));&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do until (last.MainAccount);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;by MainAccount ClientNum AlternativeAccount;&lt;/P&gt;&lt;P&gt;if not missing(AlternativeAccount) then num_client+first.AlternativeAccount;&lt;/P&gt;&lt;P&gt;if last.ClientNum then do; output;num_client=0;end; &lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;call missing(of aa(*));&lt;/P&gt;&lt;P&gt;drop AlternativeAccount;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc print;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Oct 2012 15:45:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106306#M29620</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2012-10-25T15:45:38Z</dc:date>
    </item>
    <item>
      <title>Re: Counting distinct within various groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106307#M29621</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yeah. Tom 's code is absolutely right and fast. I just want to rewrite it by using sub-query which is also a powerful tool of SQL as its cartesian product . Don't be offended , TOM.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data have ;

&amp;nbsp; input (a b c) ($) @@;

cards;
AA 1 A1 AA 1 A2 AA 1 A3 AA 2 A4 AA 2 A3 BB 1 B1 BB 1 B2 CC 1 .
;

proc sql ;
create table want as
select distinct a,b,count(distinct c) as cnt1,(select count(distinct c) from have where a=x.a ) as cnt2
&amp;nbsp; from have&amp;nbsp; as x
&amp;nbsp;&amp;nbsp; group by a,b
;
quit;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Oct 2012 02:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106307#M29621</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2012-10-26T02:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: Counting distinct within various groups</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106308#M29622</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks ... this helps a lot.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mahesh&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 Oct 2012 10:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Counting-distinct-within-various-groups/m-p/106308#M29622</guid>
      <dc:creator>Maheshvaran</dc:creator>
      <dc:date>2012-10-26T10:30:09Z</dc:date>
    </item>
  </channel>
</rss>

