<?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: Selecting the next to last row? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818083#M322910</link>
    <description>Had to update the post, sorry. Do you know the solution in this updated case?</description>
    <pubDate>Tue, 14 Jun 2022 13:47:24 GMT</pubDate>
    <dc:creator>SasStatistics</dc:creator>
    <dc:date>2022-06-14T13:47:24Z</dc:date>
    <item>
      <title>Selecting the next to last row for each ID group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818073#M322905</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Please note the post has been updated:&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How would I select the next to last row in SAS&lt;STRONG&gt; for each group of Column I&lt;/STRONG&gt;D? Either with data step or proc sql.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Example: So I would for each ID want the next to last observation, meaning I want observation/row 3, 5, 13.&lt;/P&gt;
&lt;TABLE width="147"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;ID&lt;/TD&gt;
&lt;TD width="83"&gt;Observation&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;12&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;13&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;14&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 14 Jun 2022 13:47:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818073#M322905</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2022-06-14T13:47:03Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the next to last row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818076#M322906</link>
      <description>&lt;P&gt;Delete the last row, then keep the new last row (which originally was the next to last row);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want1;
    set have;
    by id;
    if not last.id;
run;
data want2;
    set want1;
    by id;
    if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 13:40:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818076#M322906</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-14T13:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the next to last row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818083#M322910</link>
      <description>Had to update the post, sorry. Do you know the solution in this updated case?</description>
      <pubDate>Tue, 14 Jun 2022 13:47:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818083#M322910</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2022-06-14T13:47:24Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the next to last row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818088#M322913</link>
      <description>&lt;P&gt;The same code I gave should work. Have you tried it?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 13:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818088#M322913</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-14T13:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the next to last row for each ID group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818094#M322916</link>
      <description>&lt;P&gt;When I see a question like this I just have to ask "why do you not want the last row for each group"?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Along with details such as "What if the group only has one observation?" There is no "next to last" in that case.&lt;/P&gt;
&lt;P&gt;Would having all the values of other variable(s) with the same value modify the rule(s)? Or other variable(s) missing values modify the rule(s)?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 14:16:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818094#M322916</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-14T14:16:55Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the next to last row for each ID group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818176#M322952</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381436"&gt;@SasStatistics&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p00hxg3x8lwivcn1f0e9axziw57y.htm#p0u2yq8x2b1fu4n1m0d5hzam9hjv" target="_blank" rel="noopener"&gt;POINT= option&lt;/A&gt; of the SET statement to take one step back in the sequence of observations. The &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p00hxg3x8lwivcn1f0e9axziw57y.htm#p0t6q52h9jgtkbn1wqhhlk991o5f" target="_blank" rel="noopener"&gt;CUROBS= option&lt;/A&gt; helps to determine the appropriate observation number.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have curobs=_n;
by id;
if last.id &amp;amp; ~first.id then do;
  _p=_n-1;
  set have point=_p;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The condition &lt;FONT face="courier new,courier"&gt;~first.id&lt;/FONT&gt; excludes IDs with only one observation from dataset WANT.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 18:09:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818176#M322952</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-06-14T18:09:14Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the next to last row for each ID group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818181#M322954</link>
      <description>&lt;P&gt;Nice work&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;and this generalizes to the case where you want the third to last row or the fourth to last row ...&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 18:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818181#M322954</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-14T18:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the next to last row for each ID group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818186#M322958</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Nice work&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;and this generalizes to the case where you want the third to last row or the fourth to last row ...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks. That generalization would likely involve the LAG&lt;EM&gt;n&lt;/EM&gt; function (to compare IDs) or a counter variable instead of the &lt;FONT face="courier new,courier"&gt;~first.id&lt;/FONT&gt; condition.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 18:32:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818186#M322958</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-06-14T18:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting the next to last row for each ID group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818281#M322998</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input ID Observation;
cards;
1 1
1 2
1 3
1 4
2 5
2 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
3 14
;

data want;
do i=1 by 1 until(last.id);
  set have;
  by id;
end;
do j=1 by 1 until(last.id);
  set have;
  by id;
  if j=i-1  then output;
end;
drop i j;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jun 2022 12:20:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-the-next-to-last-row-for-each-ID-group/m-p/818281#M322998</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-06-15T12:20:37Z</dc:date>
    </item>
  </channel>
</rss>

