<?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: Select rows by event within a group by iterating through rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676067#M203796</link>
    <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=p);
   do _N_ = 1 by 1 until (last.subject);
      set have;
      by subject;
      if service = "heart" then p = _N_;
   end;

   if not p then p=_N_;

   do _N_ = 1 to _N_;
      set have;
      censored = (service = "heart");
      if _N_ = p then output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 12 Aug 2020 05:09:01 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2020-08-12T05:09:01Z</dc:date>
    <item>
      <title>Select rows by event within a group by iterating through rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676066#M203795</link>
      <description>&lt;P&gt;HI everyone,&lt;/P&gt;
&lt;P&gt;I have a data set that looks like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input nr subject service $ weight encounter :yymmdd10.;
format encounter yymmddd10.;
datalines;
1 1 heart 67 20130101
2 1 lung 67 20130131
3 1 gastro  69 20130302
4 1 heart 68 20130401
5 1 neuro 66 20130501
6 2 neuro 80 20130531
7 2 neuro 82 20130630
8 2 neuro 82 20130730
9 2 neuro 81 20130829
10 2 neuro 83 20130928
11 2 neuro 82 20131028
12 3 lung 55 20131127
13 3 lung 55 20131227
14 3 gastric 57 20140126
15 3 heart 57 20140225
16 3 lung 55 20140327
17 4 neuro 88 20140426
18 4 neuro 88 20140526
19 4 neuro 88 20140625
20 4 lung 86 20140725
21 4 lung 86 20140824
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am trying to clean this dataset and use it for survival analysis, basically, I am trying to come up with a data set that contains the variables&amp;nbsp;nr, subject, service,weight and encounter with one row per subject.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I will need to select the row that corresponds to the last encounter with service 'heart' for each subject, for subject that have not had an encounter with service 'heart' I need to select the latest encounter date for that subject.&lt;/P&gt;
&lt;P&gt;I also need to have a new variable called 'censored' equal to 0 if the subject had an encounter with hear and 1 if not. SO the output should look like this:&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 288pt;" border="0" width="384" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" class="xl63" style="height: 15.0pt; width: 48pt;"&gt;nr&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt;"&gt;subject&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt;"&gt;service&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt;"&gt;weight&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt;"&gt;encounter&lt;/TD&gt;
&lt;TD width="64" class="xl63" style="width: 48pt;"&gt;censored&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl63" style="height: 15.0pt;"&gt;4&lt;/TD&gt;
&lt;TD class="xl63"&gt;1&lt;/TD&gt;
&lt;TD class="xl63"&gt;heart&lt;/TD&gt;
&lt;TD class="xl63"&gt;68&lt;/TD&gt;
&lt;TD class="xl63"&gt;20130401&lt;/TD&gt;
&lt;TD class="xl63"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl63" style="height: 15.0pt;"&gt;11&lt;/TD&gt;
&lt;TD class="xl63"&gt;2&lt;/TD&gt;
&lt;TD class="xl63"&gt;neuro&lt;/TD&gt;
&lt;TD class="xl63"&gt;82&lt;/TD&gt;
&lt;TD class="xl63"&gt;20131028&lt;/TD&gt;
&lt;TD class="xl63"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl63" style="height: 15.0pt;"&gt;15&lt;/TD&gt;
&lt;TD class="xl63"&gt;3&lt;/TD&gt;
&lt;TD class="xl63"&gt;heart&lt;/TD&gt;
&lt;TD class="xl63"&gt;57&lt;/TD&gt;
&lt;TD class="xl63"&gt;20140225&lt;/TD&gt;
&lt;TD class="xl63"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" class="xl63" style="height: 15.0pt;"&gt;21&lt;/TD&gt;
&lt;TD class="xl63"&gt;4&lt;/TD&gt;
&lt;TD class="xl63"&gt;lung&lt;/TD&gt;
&lt;TD class="xl63"&gt;86&lt;/TD&gt;
&lt;TD class="xl63"&gt;20140824&lt;/TD&gt;
&lt;TD class="xl63"&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;Now this is doable with a few proc sqls but I was wondering if anyone could give an easier solution maybe using a data-- set -- by combined with arrays and&amp;nbsp; a do loop?&lt;/P&gt;
&lt;P&gt;Thank you\&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 04:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676066#M203795</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2020-08-12T04:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows by event within a group by iterating through rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676067#M203796</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=p);
   do _N_ = 1 by 1 until (last.subject);
      set have;
      by subject;
      if service = "heart" then p = _N_;
   end;

   if not p then p=_N_;

   do _N_ = 1 to _N_;
      set have;
      censored = (service = "heart");
      if _N_ = p then output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Aug 2020 05:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676067#M203796</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-12T05:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows by event within a group by iterating through rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676068#M203797</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;: impressive&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 05:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676068#M203797</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-08-12T05:20:05Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows by event within a group by iterating through rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676071#M203798</link>
      <description>Very nice.&lt;BR /&gt;Thanks for taking the time to answer. &lt;BR /&gt;</description>
      <pubDate>Wed, 12 Aug 2020 05:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676071#M203798</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2020-08-12T05:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows by event within a group by iterating through rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676098#M203811</link>
      <description>&lt;P&gt;Anytime. Thank you for a clear question.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 09:09:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676098#M203811</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-12T09:09:00Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows by event within a group by iterating through rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676099#M203812</link>
      <description>&lt;P&gt;A well-stated question is the fast path to a working answer. Kudos.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 09:20:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows-by-event-within-a-group-by-iterating-through-rows/m-p/676099#M203812</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-12T09:20:17Z</dc:date>
    </item>
  </channel>
</rss>

