<?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 get count of occurrences of a record in Developers</title>
    <link>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4807#M2011</link>
    <description>Hi, Cynthia,&lt;BR /&gt;
&lt;BR /&gt;
What a lengthy response!  Yes, I was aware of the possible permutation of all the BY variables.... however, all I wanted was a count of the duplicated records, which at this moment in my program's evolution is the simplest count to get.  I'll read and re-read your reply and study it also.&lt;BR /&gt;
&lt;BR /&gt;
Thanks for replying!</description>
    <pubDate>Mon, 24 Sep 2007 18:24:55 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2007-09-24T18:24:55Z</dc:date>
    <item>
      <title>How to get count of occurrences of a record</title>
      <link>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4805#M2009</link>
      <description>In a stored process I'm writing , in a data step, I want to get a count of how many times a record occurs.  I might have more than one record for the same entity, so I'd like to know how many times that entity occurs.&lt;BR /&gt;
&lt;BR /&gt;
I have code similar to the following (This is pseudocode...I'm aware it's not exactly syntactically correct):&lt;BR /&gt;
&lt;BR /&gt;
Data Step01;&lt;BR /&gt;
By field1 field2 field3 field4 field5;&lt;BR /&gt;
count = 1;&lt;BR /&gt;
If first.field5 then counter = 0;&lt;BR /&gt;
counter+count;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
However, the field titled counter never gets reset to zero....it just keeps incrementing.  I want it to look like this:&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
&lt;BR /&gt;
And that way, in a later data step, I can get just the last record, that'll have the total line count for that record.&lt;BR /&gt;
&lt;BR /&gt;
If someone can help out with my confusion, I'd be most appreciative.</description>
      <pubDate>Mon, 24 Sep 2007 14:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4805#M2009</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2007-09-24T14:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to get count of occurrences of a record</title>
      <link>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4806#M2010</link>
      <description>Hi:&lt;BR /&gt;
  I would think that with your BY statement you would see some incrementing, but not much. When you show that single column like this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
1&lt;BR /&gt;
2&lt;BR /&gt;
3&lt;BR /&gt;
4&lt;BR /&gt;
5&lt;BR /&gt;
[/pre]&lt;BR /&gt;
You are not taking into account the OTHER 4 BY variables.&lt;BR /&gt;
   &lt;BR /&gt;
For example, field1 is the primary, within field1, you sort by field2, then within field1 and field2, by field3, etc. so field5 is at the very end of the sort. Depending on your number of obs, you could have a new field5 on every row. Consider the following data and program:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data info;&lt;BR /&gt;
  length petowner $10 field1 field2 field3 field4 field5 $12;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input field1 $ field2 $ field3 $ field4 $  field5 $ petowner $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
cat black female indoor apartment alan&lt;BR /&gt;
cat black female indoor apartment bob&lt;BR /&gt;
cat black female indoor apartment carl&lt;BR /&gt;
cat black female indoor house dave&lt;BR /&gt;
cat black female indoor house edna&lt;BR /&gt;
cat black female outdoor apartment fiona&lt;BR /&gt;
cat black female outdoor house george&lt;BR /&gt;
cat black male indoor apartment harry&lt;BR /&gt;
cat black male indoor apartment iona&lt;BR /&gt;
cat black male indoor apartment jack&lt;BR /&gt;
cat black male outdoor apartment kathy&lt;BR /&gt;
cat black male outdoor apartment louise&lt;BR /&gt;
cat black male outdoor house mary&lt;BR /&gt;
cat white female indoor apartment nora&lt;BR /&gt;
cat white female indoor house otis&lt;BR /&gt;
cat white male indoor apartment peter&lt;BR /&gt;
cat white male indoor apartment quentin&lt;BR /&gt;
cat white male indoor house rob&lt;BR /&gt;
dog brown female indoor apartment steve&lt;BR /&gt;
dog brown female outdoor house talia&lt;BR /&gt;
dog  brown female outdoor house una&lt;BR /&gt;
dog spots female indoor house victor&lt;BR /&gt;
dog spots female outdoor house william&lt;BR /&gt;
dog spots male indoor house xavier&lt;BR /&gt;
dog white female outdoor house yarrow&lt;BR /&gt;
dog white male indoor apartment zach&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
     &lt;BR /&gt;
proc sort data=info out=info;&lt;BR /&gt;
  by field1 field2 field3 field4 field5;&lt;BR /&gt;
run;&lt;BR /&gt;
   &lt;BR /&gt;
data counters;&lt;BR /&gt;
  set info;&lt;BR /&gt;
  by field1 field2 field3 field4 field5;&lt;BR /&gt;
  retain f1cnt f2cnt f3cnt f4cnt f5cnt totcnt;&lt;BR /&gt;
  if _n_ = 1 then totcnt = 0;&lt;BR /&gt;
  if first.field1 then f1cnt=0;&lt;BR /&gt;
  if first.field2 then f2cnt=0;&lt;BR /&gt;
  if first.field3 then f3cnt=0;&lt;BR /&gt;
  if first.field4 then f4cnt=0;&lt;BR /&gt;
  if first.field5 then f5cnt=0;&lt;BR /&gt;
  totcnt + 1;&lt;BR /&gt;
  f1cnt + 1;&lt;BR /&gt;
  f2cnt + 1;&lt;BR /&gt;
  f3cnt + 1;&lt;BR /&gt;
  f4cnt + 1;&lt;BR /&gt;
  f5cnt + 1;&lt;BR /&gt;
run;&lt;BR /&gt;
    &lt;BR /&gt;
proc print data=counters;&lt;BR /&gt;
  var field1 f1cnt field2 f2cnt field3 f3cnt field4 f4cnt field5 f5cnt totcnt petowner ;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
The output from the above program is shown below:&lt;BR /&gt;
[pre]&lt;BR /&gt;
      &lt;BR /&gt;
&lt;BR /&gt;
    Obs field1 f1cnt field2 f2cnt field3 f3cnt field4  f4cnt field5    f5cnt totcnt petowner&lt;BR /&gt;
&lt;BR /&gt;
      1  cat      1  black     1  female   1   indoor    1   apartment   1      1   alan&lt;BR /&gt;
      2  cat      2  black     2  female   2   indoor    2   apartment   2      2   bob&lt;BR /&gt;
      3  cat      3  black     3  female   3   indoor    3   apartment   3      3   carl&lt;BR /&gt;
      4  cat      4  black     4  female   4   indoor    4   house       1      4   dave&lt;BR /&gt;
      5  cat      5  black     5  female   5   indoor    5   house       2      5   edna&lt;BR /&gt;
      6  cat      6  black     6  female   6   outdoor   1   apartment   1      6   fiona&lt;BR /&gt;
      7  cat      7  black     7  female   7   outdoor   2   house       1      7   george&lt;BR /&gt;
      8  cat      8  black     8  male     1   indoor    1   apartment   1      8   harry&lt;BR /&gt;
      9  cat      9  black     9  male     2   indoor    2   apartment   2      9   iona&lt;BR /&gt;
     10  cat     10  black    10  male     3   indoor    3   apartment   3     10   jack&lt;BR /&gt;
     11  cat     11  black    11  male     4   outdoor   1   apartment   1     11   kathy&lt;BR /&gt;
     12  cat     12  black    12  male     5   outdoor   2   apartment   2     12   louise&lt;BR /&gt;
     13  cat     13  black    13  male     6   outdoor   3   house       1     13   mary&lt;BR /&gt;
     14  cat     14  white     1  female   1   indoor    1   apartment   1     14   nora&lt;BR /&gt;
     15  cat     15  white     2  female   2   indoor    2   house       1     15   otis&lt;BR /&gt;
     16  cat     16  white     3  male     1   indoor    1   apartment   1     16   peter&lt;BR /&gt;
     17  cat     17  white     4  male     2   indoor    2   apartment   2     17   quentin&lt;BR /&gt;
     18  cat     18  white     5  male     3   indoor    3   house       1     18   rob&lt;BR /&gt;
     19  dog      1  brown     1  female   1   indoor    1   apartment   1     19   steve&lt;BR /&gt;
     20  dog      2  brown     2  female   2   outdoor   1   house       1     20   talia&lt;BR /&gt;
     21  dog      3  brown     3  female   3   outdoor   2   house       2     21   una&lt;BR /&gt;
     22  dog      4  spots     1  female   1   indoor    1   house       1     22   victor&lt;BR /&gt;
     23  dog      5  spots     2  female   2   outdoor   1   house       1     23   william&lt;BR /&gt;
     24  dog      6  spots     3  male     1   indoor    1   house       1     24   xavier&lt;BR /&gt;
     25  dog      7  white     1  female   1   outdoor   1   house       1     25   yarrow&lt;BR /&gt;
     26  dog      8  white     2  male     1   indoor    1   apartment   1     26   zach&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Note how totcnt correctly shows 26 and the f1cnt variable shows 18 cats and 8 dogs. Then f2cnt shows that you have 13 black cats and 5 white cats; 3 brown dogs, 3 dogs with spots and 2 white dogs -- so f2cnt variable is changing, but WITHIN field1. So by the time you get to field5 (whether the petowner lives in a house or apartment), then the count changes based on sort order for the 4 variables (field1, field2, field3, field4) that preceded field5 in the BY statement.&lt;BR /&gt;
&lt;BR /&gt;
If you wanted the count of petowners in houses vs petowners in apartments, then you'd have to change the BY statement in your SORT and in your PROGRAM. You say you want to get a total line count for a "record" assuming there are duplicates for a unique set of BY variables. &lt;BR /&gt;
&lt;BR /&gt;
For example, if you had true duplicate rows then you might get completely different counts, depending on your BY variables. Run this from a code node in EG to see the difference between what's above and with different data (with duplicate "records" except for field2):&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data infodups;&lt;BR /&gt;
  length  field1 $12 field2 $4 field3 field4 field5 $12 type wherelive $10;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input field1 $ field2 $ field3 $ field4 $  field5 $ type $ wherelive;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
alan 1982 cat 	black	female	indoor	apartment	&lt;BR /&gt;
alan 1984 cat 	black	female	indoor	apartment	&lt;BR /&gt;
alan 1996 cat 	black	female	indoor	apartment	&lt;BR /&gt;
alan 1998 cat 	black	female	indoor	apartment	&lt;BR /&gt;
alan 2006 cat 	black	female	indoor	apartment	&lt;BR /&gt;
bob 1985 cat 	black	female	indoor	apartment	&lt;BR /&gt;
bob 1987 cat 	black	female	indoor	apartment	&lt;BR /&gt;
bob 1989 cat 	black	female	indoor	apartment	&lt;BR /&gt;
bob 2000 cat 	black	female	indoor	apartment	&lt;BR /&gt;
bob 2004 cat 	black	female	indoor	apartment	&lt;BR /&gt;
bob 2007 cat 	black	female	indoor	apartment	&lt;BR /&gt;
carl 1992 cat 	black	female	indoor	apartment	&lt;BR /&gt;
carl 1994 cat 	black	female	indoor	apartment	&lt;BR /&gt;
carl 1996 cat 	black	female	indoor	apartment	&lt;BR /&gt;
carl 1999 cat 	black	female	indoor	apartment	&lt;BR /&gt;
carl 2001 cat 	black	female	indoor	apartment	&lt;BR /&gt;
carl 2007 cat 	black	female	indoor	apartment	&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
    &lt;BR /&gt;
proc sort data=infodups out=infodups;&lt;BR /&gt;
by field1 field2 field3 field4 field5;&lt;BR /&gt;
run;&lt;BR /&gt;
    &lt;BR /&gt;
data diffcntr;&lt;BR /&gt;
  set infodups;&lt;BR /&gt;
  by field1 field2 field3 field4 field5;&lt;BR /&gt;
  retain f1cnt f2cnt f3cnt f4cnt f5cnt totcnt;&lt;BR /&gt;
  if _n_ = 1 then totcnt = 0;&lt;BR /&gt;
  if first.field1 then f1cnt=0;&lt;BR /&gt;
  if first.field2 then f2cnt=0;&lt;BR /&gt;
  if first.field3 then f3cnt=0;&lt;BR /&gt;
  if first.field4 then f4cnt=0;&lt;BR /&gt;
  if first.field5 then f5cnt=0;&lt;BR /&gt;
  totcnt + 1;&lt;BR /&gt;
  f1cnt + 1;&lt;BR /&gt;
  f2cnt + 1;&lt;BR /&gt;
  f3cnt + 1;&lt;BR /&gt;
  f4cnt + 1;&lt;BR /&gt;
  f5cnt + 1;&lt;BR /&gt;
run;&lt;BR /&gt;
         &lt;BR /&gt;
proc print data=diffcntr;&lt;BR /&gt;
title 'With Real Duplicate Records';&lt;BR /&gt;
var field1 f1cnt field2 f2cnt field3 f3cnt field4 f4cnt field5 f5cnt totcnt type wherelive ;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
You might consider contacting Tech Support, because there might be another way to accomplish what you want to do with your stored process...depending on what it is that you want to do.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Mon, 24 Sep 2007 18:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4806#M2010</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2007-09-24T18:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to get count of occurrences of a record</title>
      <link>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4807#M2011</link>
      <description>Hi, Cynthia,&lt;BR /&gt;
&lt;BR /&gt;
What a lengthy response!  Yes, I was aware of the possible permutation of all the BY variables.... however, all I wanted was a count of the duplicated records, which at this moment in my program's evolution is the simplest count to get.  I'll read and re-read your reply and study it also.&lt;BR /&gt;
&lt;BR /&gt;
Thanks for replying!</description>
      <pubDate>Mon, 24 Sep 2007 18:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4807#M2011</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2007-09-24T18:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to get count of occurrences of a record</title>
      <link>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4808#M2012</link>
      <description>You could use a simple PROC FREQ. That does the counts for you. Why re-invent the wheel?&lt;BR /&gt;
&lt;BR /&gt;
Or you could use PROC SQL.&lt;BR /&gt;
&lt;BR /&gt;
Something along the lines of:&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
SELECT DISTINCT subject, COUNT(field5) AS count&lt;BR /&gt;
FROM data&lt;BR /&gt;
GROUP BY subject;&lt;BR /&gt;
QUIT;&lt;BR /&gt;
&lt;BR /&gt;
This will give you the total number of records for FIELD5 for each subject.</description>
      <pubDate>Thu, 27 Sep 2007 14:17:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/How-to-get-count-of-occurrences-of-a-record/m-p/4808#M2012</guid>
      <dc:creator>LawrenceHW</dc:creator>
      <dc:date>2007-09-27T14:17:50Z</dc:date>
    </item>
  </channel>
</rss>

