<?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 do I find first row of last group in SAS, where ordering matters? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753441#M237463</link>
    <description>&lt;P&gt;Something like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID TIME FLAG;
  datalines;
1 2 1
1 3 1
1 4 1
1 5 0
1 6 1
1 7 0
1 8 1
1 9 1
1 10 1
2 2 0
2 3 1
2 4 1
2 5 1
2 6 1
2 7 1
;

data inter;
  set have;
  by id time;
  if first.id then Cluster=0;
  if lag(flag) ne flag then Cluster+1;
run;

proc sort data=inter;
  by id descending cluster time;
run;

data want;
  set inter;
  by id;
  if first.id then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1626080014872.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61123iA2D2CC7535FF526D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1626080014872.png" alt="Patrick_0-1626080014872.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Jul 2021 08:53:43 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2021-07-12T08:53:43Z</dc:date>
    <item>
      <title>How do I find first row of last group in SAS, where ordering matters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753438#M237460</link>
      <description>&lt;P&gt;I'd like to ask help in this, as I am new to SAS, but a PROC SQL approach is usable as well.&lt;/P&gt;&lt;P&gt;My dataset has IDs, a time variable, and a flag. After I sort by id and time, I need to find the first flagged observation of the last flagged group/streak. As in:&lt;/P&gt;&lt;PRE&gt;ID TIME FLAG
1   2    1
1   3    1
1   4    1
1   5    0
1   6    1
1   7    0
1   8    1
1   9    1
1  10    1
2   2    0
2   3    1
2   4    1
2   5    1
2   6    1
2   7    1&lt;/PRE&gt;&lt;P&gt;Here I want my script to return the row where time is 8 for ID 1, as it is the first observation from the last "streak", or flagged group. For ID 2 it should be where time is 3.&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;ID TIME FLAG
1   8    1
2   3    1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm trying to wrap my head around using first. and last. here, but I suppose the problem here is that I view temporally displaced flagged groups/streaks as different groups, while SAS looks at them as they are only separated by flag, so a simple "take first. from last." is not sufficient.&lt;/P&gt;&lt;P&gt;I was also thinking of collapsing the flags to a string and using a regex lookahead, but I couldn't come up with either the method or the pattern.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 08:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753438#M237460</guid>
      <dc:creator>kgym</dc:creator>
      <dc:date>2021-07-12T08:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find first row of last group in SAS, where ordering matters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753441#M237463</link>
      <description>&lt;P&gt;Something like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID TIME FLAG;
  datalines;
1 2 1
1 3 1
1 4 1
1 5 0
1 6 1
1 7 0
1 8 1
1 9 1
1 10 1
2 2 0
2 3 1
2 4 1
2 5 1
2 6 1
2 7 1
;

data inter;
  set have;
  by id time;
  if first.id then Cluster=0;
  if lag(flag) ne flag then Cluster+1;
run;

proc sort data=inter;
  by id descending cluster time;
run;

data want;
  set inter;
  by id;
  if first.id then output;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1626080014872.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61123iA2D2CC7535FF526D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1626080014872.png" alt="Patrick_0-1626080014872.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 08:53:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753441#M237463</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-07-12T08:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find first row of last group in SAS, where ordering matters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753443#M237464</link>
      <description>Wow, thank you for the fast reply. So if I understand it correctly, the code finds clusters based on flag changes, sorts it descending based on cluster, and simply pulls the first observation. Splendid!</description>
      <pubDate>Mon, 12 Jul 2021 09:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753443#M237464</guid>
      <dc:creator>kgym</dc:creator>
      <dc:date>2021-07-12T09:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find first row of last group in SAS, where ordering matters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753445#M237466</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381743"&gt;@kgym&lt;/a&gt;&amp;nbsp;Yes, you've got it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This sort of problem where the row sequence is of relevance is much easier to solve with a SAS data step than with SQL (because the SAS data step processes the data sequentially).&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jul 2021 09:12:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753445#M237466</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-07-12T09:12:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I find first row of last group in SAS, where ordering matters?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753470#M237479</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID TIME FLAG;
  datalines;
1 2 1
1 3 1
1 4 1
1 5 0
1 6 1
1 7 0
1 8 1
1 9 1
1 10 1
2 2 0
2 3 1
2 4 1
2 5 1
2 6 1
2 7 1
;
data want;
 do until(last.flag);
  set have;
  by id flag notsorted;
  if first.flag then want_time=time;
 end;
 
 do until(last.flag);
  set have;
  by id flag notsorted;
  if last.id then output;
 end;
 drop time ;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jul 2021 12:13:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-find-first-row-of-last-group-in-SAS-where-ordering/m-p/753470#M237479</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-07-12T12:13:14Z</dc:date>
    </item>
  </channel>
</rss>

