<?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: Count observations in BY group in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516215#M3103</link>
    <description>&lt;P&gt;Not easily readable&lt;/P&gt;</description>
    <pubDate>Tue, 27 Nov 2018 03:46:23 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-11-27T03:46:23Z</dc:date>
    <item>
      <title>Count observations in BY group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516214#M3102</link>
      <description>&lt;P&gt;INPUT&lt;BR /&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Item code&amp;nbsp; &amp;nbsp; &amp;nbsp;Profile&lt;BR /&gt;000000196 212543179&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;000000270 216630523&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;000000346 217007122&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;000000395 000021678&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;000000752 000294850&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;000001277 000000344&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;000001277 000000546&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to have extra two columns, one is Count, which calculates how many times the same ID number appears in the list. Want to be use Profile divided by Count.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;OUTPUT&lt;BR /&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Item code&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Profile Count Want&lt;BR /&gt;000000196 212543179&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 1 1&lt;BR /&gt;000000270 216630523&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 1 1&lt;BR /&gt;000000346 217007122&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 1 1&lt;BR /&gt;000000395 000021678&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 1 1&lt;BR /&gt;000000752 000294850&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 1 1&lt;BR /&gt;000001277 000000344&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 2 0.5&lt;BR /&gt;000001277 000000546&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 2 0.5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 03:46:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516214#M3102</guid>
      <dc:creator>miss2223</dc:creator>
      <dc:date>2018-11-27T03:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: Count observations in BY group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516215#M3103</link>
      <description>&lt;P&gt;Not easily readable&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 03:46:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516215#M3103</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-27T03:46:23Z</dc:date>
    </item>
    <item>
      <title>Re: Count observations in BY group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516216#M3104</link>
      <description>&lt;P&gt;Sorry I just reformat it, some how it got crumbled up&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 03:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516216#M3104</guid>
      <dc:creator>miss2223</dc:creator>
      <dc:date>2018-11-27T03:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: Count observations in BY group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516218#M3105</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards truncover;
input   ID     : $20.           Item_code  : $20.  Profile ;
cards;
000000196 212543179   1
000000270 216630523   1
000000346 217007122   1
000000395 000021678   1
000000752 000294850   1
000001277 000000344   1
000001277 000000546   1
;

proc sql;
create table want as
select *,count(id) as count, profile/calculated count as want
from have
group by id;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Nov 2018 03:57:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516218#M3105</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-27T03:57:08Z</dc:date>
    </item>
    <item>
      <title>Re: Count observations in BY group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516220#M3106</link>
      <description>&lt;P&gt;If your data are sorted by ID, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards truncover;
input   ID  :$10.  Item_code  :$10.  Profile ;
cards;
000000196 212543179   1
000000270 216630523   1
000000346 217007122   1
000000395 000021678   1
000000752 000294850   1
000001277 000000344   1
000001277 000000546   1
;

data want;
  set have;
  by id;
  if last.id;                 /*Go back if not last.id     */
  count=_n_-sum(lag(_n_),0);  /*At last.id, calculate COUNT*/
  do _n_=1 to count;          /*Reread COUNT observations  */
    set have;
    want=profile/count;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step only drops down to the calculation of count when the end of a by-group is encountered.&amp;nbsp; This means that the lag(_n_) inside that calculation is only updated at the end of each successive by-group (the first lag(_n_) would be missing, so I add a zero).&amp;nbsp; Remember the lag function is not a lookback like in excel.&amp;nbsp; It's an update of a queued value, which is retained in memory across observations, until replaced by another invocation of the same lag function - i.e. at the end of the next by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then once the count value is established, a loop iterated COUNT times issues a second set statement,&amp;nbsp;re-reading the same observations, and outputs them, with the new count and want variables.&amp;nbsp; Note that each SET HAVE statement generates a separate stream of input from HAVE.&amp;nbsp; In this case the two SET's are loosely synchronized.&amp;nbsp; The first SET reads one observation at a time, while the second reads in bunches, of size count.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note even though you are reading the dataset twice, you are NOT doubling the amount of data transfer from the disk file.&amp;nbsp; Almost certainly the second SET statement is merely reading from a cache in memory generated by the first SET.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 04:38:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516220#M3106</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-11-27T04:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Count observations in BY group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516223#M3107</link>
      <description>&lt;P&gt;love this -"&lt;SPAN&gt;&amp;nbsp;Almost certainly the second SET statement is merely reading from a cache in memory generated by the first SET."&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Never knew mechanics but a lot better now&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 04:57:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516223#M3107</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-27T04:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Count observations in BY group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516359#M3127</link>
      <description>&lt;P&gt;Perhaps&lt;/P&gt;
&lt;PRE&gt;data have;
    informat ID  Itemcode $10.;
    INPUT ID  Itemcode     Profile   ;
datalines;
000000196 212543179   1
000000270 216630523   1
000000346 217007122   1
000000395 000021678   1
000000752 000294850   1
000001277 000000344   1
000001277 000000546   1
;
run;

proc sql;
   create table want as
   select a.id, a.itemcode, a.profile,b.count,(a.profile/b.count) as want
   from have as a
        left join
        (select id, count(*) as count 
         from have
         group by id) as b
      on a.id=b.id;
quit;
         &lt;/PRE&gt;
&lt;P&gt;Note use of data step to provide example data and pasted into a code box opened with the {I} icon. Also variable name ITEMCODE as SAS does not accept spaces by default in variable names. Also notice that the variable ID and Itemcode must be character to preserve leading 0s&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you required having a aggregate value Count merged with all of the base record ID the LEFT JOIN does that with the&amp;nbsp;select bit in () creating the summary.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 15:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Count-observations-in-BY-group/m-p/516359#M3127</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-11-27T15:59:04Z</dc:date>
    </item>
  </channel>
</rss>

