<?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 Need to create a variable that counts the number of previous events by ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-variable-that-counts-the-number-of-previous/m-p/849757#M335907</link>
    <description>&lt;P&gt;I have a large data set with multiple births per mother. I'm studying the occurrence of an adverse event for the infant. I was to know if the number of infants that had the adverse event previously for a given mother affects the chance of the adverse event happening for subsequent births. The first birth for each mother should always have 0 previous events, but I need to include sum the number of events after that, including if the event happens on the first birth.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input mom_id event ;&lt;BR /&gt;datalines;&lt;BR /&gt;1 0&lt;BR /&gt;1 1&lt;BR /&gt;1 0&lt;BR /&gt;2 1&lt;BR /&gt;2 1&lt;BR /&gt;2 0&lt;BR /&gt;3 0&lt;BR /&gt;3 0&lt;BR /&gt;3 0&lt;BR /&gt;4 1&lt;BR /&gt;4 1&lt;BR /&gt;4 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;These are the data I want:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input mom_id event prev_events;&lt;BR /&gt;datalines;&lt;BR /&gt;1 0 0&lt;BR /&gt;1 1 0&lt;BR /&gt;1 0 1&lt;BR /&gt;2 1 0&lt;BR /&gt;2 1 1&lt;BR /&gt;2 0 2&lt;BR /&gt;3 0 0&lt;BR /&gt;3 0 0&lt;BR /&gt;3 0 0&lt;BR /&gt;4 1 0&lt;BR /&gt;4 1 1&lt;BR /&gt;4 1 2&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a lot of experience with SAS, but not with macros or loops. Thanks for your help!&lt;/P&gt;</description>
    <pubDate>Thu, 15 Dec 2022 00:01:30 GMT</pubDate>
    <dc:creator>mdwilson</dc:creator>
    <dc:date>2022-12-15T00:01:30Z</dc:date>
    <item>
      <title>Need to create a variable that counts the number of previous events by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-variable-that-counts-the-number-of-previous/m-p/849757#M335907</link>
      <description>&lt;P&gt;I have a large data set with multiple births per mother. I'm studying the occurrence of an adverse event for the infant. I was to know if the number of infants that had the adverse event previously for a given mother affects the chance of the adverse event happening for subsequent births. The first birth for each mother should always have 0 previous events, but I need to include sum the number of events after that, including if the event happens on the first birth.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input mom_id event ;&lt;BR /&gt;datalines;&lt;BR /&gt;1 0&lt;BR /&gt;1 1&lt;BR /&gt;1 0&lt;BR /&gt;2 1&lt;BR /&gt;2 1&lt;BR /&gt;2 0&lt;BR /&gt;3 0&lt;BR /&gt;3 0&lt;BR /&gt;3 0&lt;BR /&gt;4 1&lt;BR /&gt;4 1&lt;BR /&gt;4 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;These are the data I want:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input mom_id event prev_events;&lt;BR /&gt;datalines;&lt;BR /&gt;1 0 0&lt;BR /&gt;1 1 0&lt;BR /&gt;1 0 1&lt;BR /&gt;2 1 0&lt;BR /&gt;2 1 1&lt;BR /&gt;2 0 2&lt;BR /&gt;3 0 0&lt;BR /&gt;3 0 0&lt;BR /&gt;3 0 0&lt;BR /&gt;4 1 0&lt;BR /&gt;4 1 1&lt;BR /&gt;4 1 2&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a lot of experience with SAS, but not with macros or loops. Thanks for your help!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2022 00:01:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-variable-that-counts-the-number-of-previous/m-p/849757#M335907</guid>
      <dc:creator>mdwilson</dc:creator>
      <dc:date>2022-12-15T00:01:30Z</dc:date>
    </item>
    <item>
      <title>Re: Need to create a variable that counts the number of previous events by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-variable-that-counts-the-number-of-previous/m-p/849900#M335940</link>
      <description>&lt;P&gt;I think this is what you want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input mom_id event ;
datalines;
1 0
1 1
1 0
2 1
2 1
2 0
3 0
3 0
3 0
4 1
4 1
4 1
;

data want;
   set have;
   by mom_id;
   if first.mom_id then prev_events = 0;
   prev_events + ifn(first.mom_id, ., lag(event));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;mom_id  event  prev_events
1       0      0
1       1      0
1       0      1
2       1      0
2       1      1
2       0      2
3       0      0
3       0      0
3       0      0
4       1      0
4       1      1
4       1      2&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Dec 2022 16:12:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-create-a-variable-that-counts-the-number-of-previous/m-p/849900#M335940</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-12-15T16:12:20Z</dc:date>
    </item>
  </channel>
</rss>

