<?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: how to do this in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104387#M29170</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since you will need two different grouping:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;/P&gt;&lt;P&gt;create table want_bad as &lt;/P&gt;&lt;P&gt;select a.*, b.count_sub from &lt;/P&gt;&lt;P&gt;(select *,count(distinct type) as count_type&lt;/P&gt;&lt;P&gt;from have&lt;/P&gt;&lt;P&gt;group by id) a&lt;/P&gt;&lt;P&gt;left join &lt;/P&gt;&lt;P&gt;(select *, count(type) as count_sub&lt;/P&gt;&lt;P&gt;from have&lt;/P&gt;&lt;P&gt;group by id ,type) b&lt;/P&gt;&lt;P&gt;on a.id=b.id and a.type=b.type&lt;/P&gt;&lt;P&gt;order by id,type&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Haikuo &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 04 May 2012 18:17:40 GMT</pubDate>
    <dc:creator>Haikuo</dc:creator>
    <dc:date>2012-05-04T18:17:40Z</dc:date>
    <item>
      <title>how to do this</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104386#M29169</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello ,&lt;/P&gt;&lt;P&gt;Here is a data set have,&lt;/P&gt;&lt;P&gt;The problem is I want new variable count_sub count the number of each type for each id,&lt;/P&gt;&lt;P&gt;Please help.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input id type $;&lt;BR /&gt;cards;&lt;BR /&gt;1 A&lt;BR /&gt;1 A&lt;BR /&gt;1 A&lt;BR /&gt;1 B&lt;BR /&gt;2 A&lt;BR /&gt;2 B&lt;BR /&gt;2 B&lt;BR /&gt;2 C&lt;BR /&gt;2 C&lt;BR /&gt;2 C&lt;BR /&gt;2 C&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;create table want_bad as &lt;BR /&gt;select *,count(distinct type) as count_type,count(type) as count_sub&lt;BR /&gt;from have&lt;BR /&gt;group by id&lt;BR /&gt;order by id,type&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The dataset want should be like this:&lt;/P&gt;&lt;P&gt;1 A 2 3&lt;/P&gt;&lt;P&gt;1 A 2 3&lt;/P&gt;&lt;P&gt;1 A 2 3 &lt;/P&gt;&lt;P&gt;1 B 2 1&lt;/P&gt;&lt;P&gt;2 A 3 1&lt;/P&gt;&lt;P&gt;2 B 3 2 &lt;/P&gt;&lt;P&gt;2 B 3 2&lt;/P&gt;&lt;P&gt;2 C 3 4 &lt;/P&gt;&lt;P&gt;2 C 3 4 &lt;/P&gt;&lt;P&gt;2 C 3 4&lt;/P&gt;&lt;P&gt;2 C 3 4&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 May 2012 18:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104386#M29169</guid>
      <dc:creator>Mike_Davis</dc:creator>
      <dc:date>2012-05-04T18:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to do this</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104387#M29170</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since you will need two different grouping:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;/P&gt;&lt;P&gt;create table want_bad as &lt;/P&gt;&lt;P&gt;select a.*, b.count_sub from &lt;/P&gt;&lt;P&gt;(select *,count(distinct type) as count_type&lt;/P&gt;&lt;P&gt;from have&lt;/P&gt;&lt;P&gt;group by id) a&lt;/P&gt;&lt;P&gt;left join &lt;/P&gt;&lt;P&gt;(select *, count(type) as count_sub&lt;/P&gt;&lt;P&gt;from have&lt;/P&gt;&lt;P&gt;group by id ,type) b&lt;/P&gt;&lt;P&gt;on a.id=b.id and a.type=b.type&lt;/P&gt;&lt;P&gt;order by id,type&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Haikuo &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 May 2012 18:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104387#M29170</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2012-05-04T18:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to do this</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104388#M29171</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Apparently it can be done more intuitive by using SQL, just in case you are wondering how it can be done using data step, here is one approach:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input id type $;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1 A&lt;/P&gt;&lt;P&gt;1 A&lt;/P&gt;&lt;P&gt;1 A&lt;/P&gt;&lt;P&gt;1 B&lt;/P&gt;&lt;P&gt;2 A&lt;/P&gt;&lt;P&gt;2 B&lt;/P&gt;&lt;P&gt;2 B&lt;/P&gt;&lt;P&gt;2 C&lt;/P&gt;&lt;P&gt;2 C&lt;/P&gt;&lt;P&gt;2 C&lt;/P&gt;&lt;P&gt;2 C&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;do count_type=1 by 1 until (last.id);&lt;/P&gt;&lt;P&gt;&amp;nbsp; do until (last.type);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id type;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; do until (last.id);&lt;/P&gt;&lt;P&gt;&amp;nbsp; do count_sub=1 by 1 until (last.type);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id type;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do until (last.type);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by id type;&lt;/P&gt;&lt;P&gt;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt; end;&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;Regards,&lt;/P&gt;&lt;P&gt;Haikuo &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 04 May 2012 18:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104388#M29171</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2012-05-04T18:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: how to do this</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104389#M29172</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How about:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data have;
input id type $;
cards;
1 A
1 A
1 A
1 B
2 A
2 B
2 B
2 C
2 C
2 C
2 C
;
run;
proc sql;
create table want as
 select id,type,id_sum,type_sum 
&amp;nbsp; from (select * from have left join (select id as _id,type as _type,count(*) as type_sum from have group by id,type) on id=_id and type=_type )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; left join (select id as __id,count(distinct type) as id_sum from have group by id) on id=__id ;
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;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 07 May 2012 04:23:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-do-this/m-p/104389#M29172</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2012-05-07T04:23:56Z</dc:date>
    </item>
  </channel>
</rss>

