<?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: Create a count variable by group and output total next to each element of that group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622774#M183220</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/207645"&gt;@pkopersk&lt;/a&gt;&amp;nbsp; &amp;nbsp;Your approach is correct, however you need an extra pass of the by group to assign the computed cumulative count to each record of the by group. So, Basically I have tweaked your code to read the BY group again by using a technique called interleave aka sorted append and conditionally compute the cumulative count and assign appropriately.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input VAR1 $	VAR2;*	COUNT_BY_GROUP;
cards;
A	10	2
A	20	2
B	243	1
C	4	3
C	21	3
C	4	3
;

data want;
 set have(in=a) have(in=b);
 by VAR1;
 retain COUNT_BY_GROUP;
 if a then do;
  if first.VAR1 then COUNT_BY_GROUP = 0;
  COUNT_BY_GROUP +1;
 end;
 if b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The genius&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; taught me this among many others he taught me personally over the last couple of years. Have fun learning!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 06 Feb 2020 16:43:51 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-02-06T16:43:51Z</dc:date>
    <item>
      <title>Create a count variable by group and output total next to each element of that group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622761#M183215</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am struggling with achieving something like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;VAR1&lt;/TD&gt;&lt;TD&gt;VAR2&lt;/TD&gt;&lt;TD&gt;COUNT_BY_GROUP&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;243&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;21&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where the COUNT_BY_GROUP variable is the total number of elements of that group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code that I'm currently using:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by VAR1;
retain COUNT_BY_GROUP;
if first.VAR1 then COUNT_BY_GROUP = 0;
COUNT_BY_GROUP +1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But it gives me a cumulative count. I want to save the total count of elements in a group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 16:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622761#M183215</guid>
      <dc:creator>pkopersk</dc:creator>
      <dc:date>2020-02-06T16:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create a count variable by group and output total next to each element of that group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622764#M183217</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
    tables var1/noprint out=_a_;
run;
data want;
    merge have _a_;
    by var1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PROC FREQ is where you should start when you have a problem that involves counting a number of observations in a group. It's a very good thing to learn, and a far superior way to go than writing your own data step code each time you want to perform counting.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 16:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622764#M183217</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-02-06T16:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Create a count variable by group and output total next to each element of that group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622768#M183219</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/207645"&gt;@pkopersk&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is another approach to the one proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table want as
	select VAR1, VAR2, n(VAR1) as COUNT_BY_GROUP
	from have
	group by VAR1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 16:26:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622768#M183219</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-06T16:26:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create a count variable by group and output total next to each element of that group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622774#M183220</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/207645"&gt;@pkopersk&lt;/a&gt;&amp;nbsp; &amp;nbsp;Your approach is correct, however you need an extra pass of the by group to assign the computed cumulative count to each record of the by group. So, Basically I have tweaked your code to read the BY group again by using a technique called interleave aka sorted append and conditionally compute the cumulative count and assign appropriately.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input VAR1 $	VAR2;*	COUNT_BY_GROUP;
cards;
A	10	2
A	20	2
B	243	1
C	4	3
C	21	3
C	4	3
;

data want;
 set have(in=a) have(in=b);
 by VAR1;
 retain COUNT_BY_GROUP;
 if a then do;
  if first.VAR1 then COUNT_BY_GROUP = 0;
  COUNT_BY_GROUP +1;
 end;
 if b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The genius&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; taught me this among many others he taught me personally over the last couple of years. Have fun learning!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 16:43:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-count-variable-by-group-and-output-total-next-to-each/m-p/622774#M183220</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-06T16:43:51Z</dc:date>
    </item>
  </channel>
</rss>

