<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41038#M8407</link>
    <description>Agreed - no need for the BY and FIRST/LAST processing, rather consider some technique for counting/summarizing (FREQ or SUMMARY maybe, or a DATA step, where there you would use FIRST. and LAST. to count occurences of N (not involving ID).  So work through the challenge, one step at a time, writing a piece of code to address one portion, and then assembling the pieces, like a puzzle.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
    <pubDate>Thu, 04 Jun 2009 11:54:05 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2009-06-04T11:54:05Z</dc:date>
    <item>
      <title>Counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41035#M8404</link>
      <description>My table:&lt;BR /&gt;
&lt;BR /&gt;
ID__ X&lt;BR /&gt;
4___1&lt;BR /&gt;
4___2&lt;BR /&gt;
4___2&lt;BR /&gt;
5___1&lt;BR /&gt;
7__ 2&lt;BR /&gt;
7__ 2&lt;BR /&gt;
7__ 3&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;BR /&gt;
I want it to look like this:&lt;BR /&gt;
&lt;BR /&gt;
ID__ X__N1__N2__N3&lt;BR /&gt;
4___1___1&lt;BR /&gt;
4___2___1___1&lt;BR /&gt;
4___2___1___1&lt;BR /&gt;
5___1___2___1&lt;BR /&gt;
7__ 2___2___2&lt;BR /&gt;
7__ 2___2___2&lt;BR /&gt;
7__ 3___2___2___1&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;BR /&gt;
&lt;BR /&gt;
 In NX I want the number of ID:s where X have been found.&lt;BR /&gt;
When I stopped above, X=1 had been found for ID:s 4 and 5, so N1=2.&lt;BR /&gt;
X=2 in ID:s 4 and 7, so N2=2.&lt;BR /&gt;
X=3 in ID 7, so N3=1.&lt;BR /&gt;
&lt;BR /&gt;
I &lt;B&gt;insist&lt;/B&gt; that the counting should be done in a data step.&lt;BR /&gt;
&lt;BR /&gt;
I don't think I can use FIRST or LAST.&lt;BR /&gt;
&lt;BR /&gt;
Can anyone help me?&lt;BR /&gt;
&lt;BR /&gt;
Susan</description>
      <pubDate>Thu, 04 Jun 2009 09:04:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41035#M8404</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-06-04T09:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: Counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41036#M8405</link>
      <description>Yes, it's possible with a DATA step, I would say - you want to do something of the following "like" process, but honestly, I'm still not 100% clear that I understand some of the derived observation values you've shown:&lt;BR /&gt;
&lt;BR /&gt;
1) Sort your data.&lt;BR /&gt;
2) Using PROC SQL or some other technique, derive a max value for X to a macro variable to use later with an ARRAY and getting a var range for N1-N&amp;amp;max&lt;BR /&gt;
3) Using a DATA step, do the following:&lt;BR /&gt;
3a) Read in your sorted file to create another file.&lt;BR /&gt;
3b) Setup an array with a numeric variable set, N1-N&amp;amp;max.&lt;BR /&gt;
3c) With a BY and using IF FIRST.ID and LAST.ID, increment by one the array using X as the array subscript.&lt;BR /&gt;
3d) Assign a var OBSNUM = _N_  -- must use later to merge new vars back into your original file.&lt;BR /&gt;
4) Merge your file from #3 above back onto your original file using OBSNUM as your BY variable - you will need to assign a var OBSNUM in your original file first, of course.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 04 Jun 2009 11:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41036#M8405</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-06-04T11:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41037#M8406</link>
      <description>In N(X) I want the number of ID:s(let's say individuals) where X have been found.&lt;BR /&gt;
&lt;BR /&gt;
That must be easy to understand.&lt;BR /&gt;
&lt;BR /&gt;
If there are a total of 10 ID:s, and ID=2 have e.g 754, or whatever, instances of X=3 and ID=5, have e.g 28, or whatever, instances of X=3,  and the remaining 8 individuals have no occurrence of X=3, then N(3) should be 2, because in two different ID:s there were at least one occurrence of X=3.&lt;BR /&gt;
&lt;BR /&gt;
I don't understand how FIRST.ID and LAST.ID could be of any help. Could you please explain.&lt;BR /&gt;
&lt;BR /&gt;
Susan</description>
      <pubDate>Thu, 04 Jun 2009 11:38:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41037#M8406</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-06-04T11:38:14Z</dc:date>
    </item>
    <item>
      <title>Re: Counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41038#M8407</link>
      <description>Agreed - no need for the BY and FIRST/LAST processing, rather consider some technique for counting/summarizing (FREQ or SUMMARY maybe, or a DATA step, where there you would use FIRST. and LAST. to count occurences of N (not involving ID).  So work through the challenge, one step at a time, writing a piece of code to address one portion, and then assembling the pieces, like a puzzle.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 04 Jun 2009 11:54:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41038#M8407</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-06-04T11:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: Counting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41039#M8408</link>
      <description>This should do:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data _null_;&lt;BR /&gt;
  retain NN:;&lt;BR /&gt;
  array VAL(3,7) _temporary_; * will store whether any crossing has been found;&lt;BR /&gt;
  array NN(3) ;               * will sum the crossings for a X;&lt;BR /&gt;
  input ID X;                 * read data;&lt;BR /&gt;
  VAL(X,ID)=1;                * ID/X crossing found;&lt;BR /&gt;
  NN(X)=0 ;                   * initialise NB of crossings found so far;&lt;BR /&gt;
  do IDNUM=1 to 7;            * count NB of crossings found so far;&lt;BR /&gt;
    NN(X)+ val(X,IDNUM) ;&lt;BR /&gt;
  end;&lt;BR /&gt;
  put ID X NN1-NN7;&lt;BR /&gt;
cards;&lt;BR /&gt;
4 1&lt;BR /&gt;
4 2&lt;BR /&gt;
4 2&lt;BR /&gt;
5 1&lt;BR /&gt;
7 2&lt;BR /&gt;
7 2&lt;BR /&gt;
7 3&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
It outputs:&lt;BR /&gt;
[pre]&lt;BR /&gt;
4 1 1 . . . . . .&lt;BR /&gt;
4 2 1 1 . . . . .&lt;BR /&gt;
4 2 1 1 . . . . .&lt;BR /&gt;
5 1 2 1 . . . . .&lt;BR /&gt;
7 2 2 2 . . . . .&lt;BR /&gt;
7 2 2 2 . . . . .&lt;BR /&gt;
7 3 2 2 1 . . . .</description>
      <pubDate>Mon, 08 Jun 2009 01:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting/m-p/41039#M8408</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2009-06-08T01:09:41Z</dc:date>
    </item>
  </channel>
</rss>

