<?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 count number of repeated value of a variable for each group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298385#M62745</link>
    <description>&lt;P&gt;First, add a variable to your data set so you can sort the observations, but later put them back into their original order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a program that will get you halfway there.&amp;nbsp; It will produce the counts and put them into a separate data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=have noprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; tables cityid * weather / out=summary_counts (keep=cityid weather count);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you up to the task of combining the summary data set with the original, or would you need help with that part?&lt;/P&gt;</description>
    <pubDate>Wed, 14 Sep 2016 17:20:18 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-09-14T17:20:18Z</dc:date>
    <item>
      <title>How to count number of repeated value of a variable for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298365#M62738</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I want to count how many days in a week is sunny, rainy or cloudy for each city. Anyone could give me some suggestion? Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original data is as follows:&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 108pt;" border="0" cellpadding="0" cellspacing="0" width="144"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 54pt;" span="2" width="72" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt; width: 54pt;" width="72" height="18"&gt;cityid&lt;/TD&gt;
&lt;TD class="xl63" style="width: 54pt;" width="72"&gt;weather&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;sunny&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;sunny&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;cloudy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;rainy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;rainy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;cloudy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;sunny&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;rainy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;rainy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;sunny&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;sunny&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;sunny&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;cloudy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl63" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;cloudy&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I really want the data to be is as follows:&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 162pt;" border="0" cellpadding="0" cellspacing="0" width="216"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 54pt;" span="2" width="72" /&gt; &lt;COL style="width: 54pt;" width="72" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt; width: 54pt;" width="72" height="18"&gt;cityid&lt;/TD&gt;
&lt;TD class="xl65" style="width: 54pt;" width="72"&gt;weather&lt;/TD&gt;
&lt;TD class="xl65" style="width: 54pt;" width="72"&gt;count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;sunny&lt;/TD&gt;
&lt;TD class="xl65"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;sunny&lt;/TD&gt;
&lt;TD class="xl65"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;cloudy&lt;/TD&gt;
&lt;TD class="xl65"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;rainy&lt;/TD&gt;
&lt;TD class="xl65"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;rainy&lt;/TD&gt;
&lt;TD class="xl65"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;cloudy&lt;/TD&gt;
&lt;TD class="xl65"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;1&lt;/TD&gt;
&lt;TD class="xl65"&gt;sunny&lt;/TD&gt;
&lt;TD class="xl65"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;rainy&lt;/TD&gt;
&lt;TD class="xl65"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;rainy&lt;/TD&gt;
&lt;TD class="xl65"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;sunny&lt;/TD&gt;
&lt;TD class="xl65"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;sunny&lt;/TD&gt;
&lt;TD class="xl65"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;sunny&lt;/TD&gt;
&lt;TD class="xl65"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;sunny&lt;/TD&gt;
&lt;TD class="xl65"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 13.5pt;"&gt;
&lt;TD class="xl65" style="height: 13.5pt;" height="18"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;cloudy&lt;/TD&gt;
&lt;TD class="xl65"&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 14 Sep 2016 16:24:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298365#M62738</guid>
      <dc:creator>owenwqp1</dc:creator>
      <dc:date>2016-09-14T16:24:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of repeated value of a variable for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298385#M62745</link>
      <description>&lt;P&gt;First, add a variable to your data set so you can sort the observations, but later put them back into their original order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a program that will get you halfway there.&amp;nbsp; It will produce the counts and put them into a separate data set.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=have noprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; tables cityid * weather / out=summary_counts (keep=cityid weather count);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you up to the task of combining the summary data set with the original, or would you need help with that part?&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 17:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298385#M62745</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-14T17:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of repeated value of a variable for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298396#M62754</link>
      <description>&lt;P&gt;As a one step alternative, you could try using Hash:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cityid weather$ ;
cards;
1 sunny 
1 sunny 
1 cloudy 
1 rainy 
1 rainy 
1 cloudy 
1 sunny 
2 rainy 
2 rainy 
2 sunny 
2 sunny 
2 sunny 
2 sunny
2 cloudy 
;


data want;
if _n_=1 then do;
  dcl hash h();
  h.definekey('weather');
  h.definedata('count');
  h.definedone();
  end;
do until (last.cityid);
  set have;
    by cityid notsorted ;
   rc=h.find();
   if rc=0 then count+1;
   else count= 1;
   rc=h.replace();
end;

do until (last.cityid);
  set have;
    by cityid notsorted ;
   rc=h.find();
   output;
end;

rc=h.clear();
drop rc;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Sep 2016 18:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298396#M62754</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-09-14T18:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of repeated value of a variable for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298497#M62784</link>
      <description>&lt;PRE&gt;
data have;
input cityid weather$ ;
n+1;
cards;
1 sunny 
1 sunny 
1 cloudy 
1 rainy 
1 rainy 
1 cloudy 
1 sunny 
2 rainy 
2 rainy 
2 sunny 
2 sunny 
2 sunny 
2 sunny
2 cloudy 
;
run;
proc sql;
create table want(drop=n) as
select *,count(*) as count
 from have 
  group by cityid,weather
   order by n;
quit;


&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Sep 2016 03:45:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298497#M62784</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-09-15T03:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of repeated value of a variable for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298532#M62803</link>
      <description>Thanks! 	Haikuo</description>
      <pubDate>Thu, 15 Sep 2016 07:01:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298532#M62803</guid>
      <dc:creator>owenwqp1</dc:creator>
      <dc:date>2016-09-15T07:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to count number of repeated value of a variable for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298533#M62804</link>
      <description>Thanks! Ksharp, your codes are excactly what I want!</description>
      <pubDate>Thu, 15 Sep 2016 07:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-number-of-repeated-value-of-a-variable-for-each/m-p/298533#M62804</guid>
      <dc:creator>owenwqp1</dc:creator>
      <dc:date>2016-09-15T07:02:41Z</dc:date>
    </item>
  </channel>
</rss>

