<?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 observations in by group and then assigning those observations with the total count in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559484#M10338</link>
    <description>&lt;P&gt;thanks for this! do you have an example with proc sql?&lt;/P&gt;</description>
    <pubDate>Thu, 16 May 2019 20:27:38 GMT</pubDate>
    <dc:creator>mallorybane</dc:creator>
    <dc:date>2019-05-16T20:27:38Z</dc:date>
    <item>
      <title>counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559474#M10333</link>
      <description>&lt;P&gt;Hi all!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know how to count observations by group, for example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data temp;&lt;BR /&gt;&amp;nbsp; set dataset;&lt;BR /&gt;&amp;nbsp; by&amp;nbsp;keyvar;&lt;BR /&gt;&amp;nbsp; length per 8.;&lt;BR /&gt;&amp;nbsp; if first.keyvar then per=0;&lt;BR /&gt;&amp;nbsp; per+1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This code returns output&amp;nbsp;that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;keyvar&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; per(count)&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt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TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;c&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have any ideas for me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks! Hope to hear from people!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2019 20:03:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559474#M10333</guid>
      <dc:creator>mallorybane</dc:creator>
      <dc:date>2019-05-16T20:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559477#M10334</link>
      <description>&lt;P&gt;PROC FREQ will count the observations by group, then you can merge the results with the original data. Or PROC SQL can do this as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
     tables keyvar/out=counts;
run;
data want;
    merge have counts;
    by keyvar;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 May 2019 20:06:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559477#M10334</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-16T20:06:26Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559484#M10338</link>
      <description>&lt;P&gt;thanks for this! do you have an example with proc sql?&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2019 20:27:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559484#M10338</guid>
      <dc:creator>mallorybane</dc:creator>
      <dc:date>2019-05-16T20:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559485#M10339</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/242328"&gt;@mallorybane&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input keyvar  $ ;
cards;
a	1
b	1
b	2
b	3
c	1
c	2
c	3
c	4
;

proc sql;
create table want as
select a.*,count
from 
have a, (select keyvar, count(keyvar)as count from have group by keyvar) b
where a.keyvar=b.keyvar
order by a.keyvar;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 May 2019 20:32:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559485#M10339</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-16T20:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559490#M10342</link>
      <description>&lt;P&gt;And if you are using SAS&lt;STRONG&gt; 9.4&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;
if _n_=1 then do;
   dcl hash H (ordered: "A",multidata:'y') ;
   h.definekey  ("keyvar") ;
   h.definedata ("keyvar", "count") ;
   h.definedone () ;
end;
do count=1 by 1 until(last.keyvar);
set have end=lr;
by keyvar;
rc=h.add();
end;
rc=h.replace();
if lr then h.output(dataset:'want')	;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 May 2019 13:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559490#M10342</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-17T13:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559491#M10343</link>
      <description>&lt;P&gt;Simple double DOW, prolly the fastest&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
if 0 then set have;
do count=1 by 1 until(last.keyvar);
set have ;
by keyvar;
end;
do until(last.keyvar);
set have;
by keyvar;
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 May 2019 21:02:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559491#M10343</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-16T21:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559492#M10344</link>
      <description>&lt;P&gt;Using FIRST and LAST but interleave&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have(in=a) have(in=b);
by keyvar;
if first.keyvar then count=0;
if a then count+1;
if b then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 May 2019 21:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559492#M10344</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-16T21:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559547#M10352</link>
      <description>&lt;P&gt;Very nice &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; !&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 03:36:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559547#M10352</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-05-17T03:36:58Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559548#M10353</link>
      <description>&lt;P&gt;Thank you Sir&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;. Hopefully as good as you one day however a farfetched dream as of now. &lt;STRONG&gt;Merci beacoup!&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 May 2019 03:38:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559548#M10353</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-17T03:38:31Z</dc:date>
    </item>
    <item>
      <title>Re: counting observations in by group and then assigning those observations with the total count</title>
      <link>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559665#M10373</link>
      <description>&lt;P&gt;Make a dummy variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input keyvar  $ ;
dummy=1;
cards;
a	1
b	1
b	2
b	3
c	1
c	2
c	3
c	4
;

proc sql;
create table want(drop=dummy) as
 select *,count(*) as count
  from have
   group by keyvar;
 quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 May 2019 14:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/counting-observations-in-by-group-and-then-assigning-those/m-p/559665#M10373</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-17T14:16:43Z</dc:date>
    </item>
  </channel>
</rss>

