<?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: Need to count number of instances per id in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757571#M30136</link>
    <description>&lt;P&gt;ID2 has the following sequence:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;ID&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;Injury&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;Prev_Injury&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;0&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So why is your injury_count sequence&amp;nbsp; &amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;0 (for first row), 1 (second row), 1 (3rd row)&lt;/STRONG&gt;&lt;/EM&gt;, instead of &lt;EM&gt;&lt;STRONG&gt;0,1,2&lt;/STRONG&gt;&lt;/EM&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the role, if any, of the previous_injury column?&lt;/P&gt;</description>
    <pubDate>Wed, 28 Jul 2021 01:37:21 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-07-28T01:37:21Z</dc:date>
    <item>
      <title>Need to count number of instances per id</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757562#M30134</link>
      <description>&lt;P&gt;Hi! I'm trying to create an output that counts the number of times an individual has had an injury. I am working with data similar to dataset have, but I want a fourth column starts counting fresh for each new id. How do I go about doing this? I'm on SAS Enterprise.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id injury previous_injury;
datalines;
1	0	0
1	0	0
1	1	0
1	0	1
1	1	1
2	0	0
2	1	0
2	1	1
3	1	0
3	0	1
3	0	1
4	0	0
4	1	0
4	0	1
4	1	1
;
run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input id injury previous_injury count_injury;&lt;BR /&gt;datalines;&lt;BR /&gt;1 0 0 0&lt;BR /&gt;1 0 0 0&lt;BR /&gt;1 1 0 1&lt;BR /&gt;1 0 1 1&lt;BR /&gt;1 1 1 2&lt;BR /&gt;2 0 0 0&lt;BR /&gt;2 1 0 0&lt;BR /&gt;2 1 1 1&lt;BR /&gt;3 1 0 1&lt;BR /&gt;3 0 1 1&lt;BR /&gt;3 0 1 1&lt;BR /&gt;4 0 0 0&lt;BR /&gt;4 1 0 1&lt;BR /&gt;4 1 1 2&lt;BR /&gt;4 1 1 3&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I've tried some sql coding and the data step using the retain statement. The closest I've gotten is a single count for an id number (ex. each row for id 4 would say 3).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jul 2021 00:05:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757562#M30134</guid>
      <dc:creator>erin3</dc:creator>
      <dc:date>2021-07-28T00:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of instances per id</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757563#M30135</link>
      <description>&lt;P&gt;Yikes! I'm not sure why that didn't post right. Please see this code instead.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;
input id injury previous_injury;
datalines;
1	0	0
1	0	0
1	1	0
1	0	1
1	1	1
2	0	0
2	1	0
2	1	1
3	1	0
3	0	1
3	0	1
4	0	0
4	1	0
4	0	1
4	1	1
;
run;

data want;
input id injury previous_injury count_injury;
datalines;
1	0	0	0
1	0	0	0
1	1	0	1
1	0	1	1
1	1	1	2
2	0	0	0
2	1	0	0
2	1	1	1
3	1	0	1
3	0	1	1
3	0	1	1
4	0	0	0
4	1	0	1
4	1	1	2
4	1	1	3
;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Jul 2021 00:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757563#M30135</guid>
      <dc:creator>erin3</dc:creator>
      <dc:date>2021-07-28T00:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of instances per id</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757571#M30136</link>
      <description>&lt;P&gt;ID2 has the following sequence:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;ID&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;Injury&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;Prev_Injury&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;0&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="33.333333333333336%"&gt;2&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;TD width="33.333333333333336%"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So why is your injury_count sequence&amp;nbsp; &amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;0 (for first row), 1 (second row), 1 (3rd row)&lt;/STRONG&gt;&lt;/EM&gt;, instead of &lt;EM&gt;&lt;STRONG&gt;0,1,2&lt;/STRONG&gt;&lt;/EM&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the role, if any, of the previous_injury column?&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jul 2021 01:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757571#M30136</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-28T01:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of instances per id</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757692#M30155</link>
      <description>&lt;P&gt;Previous in jury was just another variable of interest in the work flow and I wasn't sure if it was useful in building the count variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You're right though! The injury count sequence for id2 should be 0, 1, 2. My tired brain wrote the example incorrectly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can the problem still be solved? &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jul 2021 13:56:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757692#M30155</guid>
      <dc:creator>erin3</dc:creator>
      <dc:date>2021-07-28T13:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of instances per id</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757716#M30156</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/391336"&gt;@erin3&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Previous in jury was just another variable of interest in the work flow and I wasn't sure if it was useful in building the count variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're right though! The injury count sequence for id2 should be 0, 1, 2. My tired brain wrote the example incorrectly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can the problem still be solved? &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, most easily through use of a "summing statement":&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  if first.id then injury_count=0;
  injury_count+injury;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  injury_count+injury;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is a "summing statement".&amp;nbsp; Unlike the statement "injury_count=injury_count+injury", it primarily does two things&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Adds the value of injury to the rolling total of injury_count and stores the result in injury_count.&amp;nbsp; It will treat a starting value of injury_count of missing (".") as a zero if the addend injury is not also missing.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Tells SAS to retain (i.e. carry over) the injury_count value from one observation to the next.&amp;nbsp; This gets the rolling total.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The "&lt;EM&gt;&lt;STRONG&gt;set have; by id;&lt;/STRONG&gt;&lt;/EM&gt;" statements tell SAS to expect the incoming data to be sorted by ID.&amp;nbsp; It generates the temporary dummies &lt;EM&gt;&lt;STRONG&gt;first.id&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.id&lt;/STRONG&gt;&lt;/EM&gt;, so that you can test whether the observation in hand is at the start or end of a given BY variable group.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jul 2021 14:38:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Need-to-count-number-of-instances-per-id/m-p/757716#M30156</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-28T14:38:03Z</dc:date>
    </item>
  </channel>
</rss>

