<?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 last second value for each group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/last-second-value-for-each-group/m-p/935890#M367915</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data b;
input cc swipe;
cards;
1 200
1 300
1 200
1 900
2 1300
2 1400
2 1500
;
run;




proc sort data=b;
by cc;
run;


data last_second;
  set b;
  by cc;
last_second_value = lag(swipe);
if last.cc then do;
    output;
  end;
  drop swipe;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&amp;nbsp;How to get last second values using PROC SQL Method &lt;BR /&gt;output &lt;BR /&gt;cc swipe&lt;BR /&gt;1 200&lt;BR /&gt;2 1400&lt;/PRE&gt;</description>
    <pubDate>Tue, 16 Jul 2024 09:52:43 GMT</pubDate>
    <dc:creator>pavank</dc:creator>
    <dc:date>2024-07-16T09:52:43Z</dc:date>
    <item>
      <title>last second value for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-second-value-for-each-group/m-p/935890#M367915</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data b;
input cc swipe;
cards;
1 200
1 300
1 200
1 900
2 1300
2 1400
2 1500
;
run;




proc sort data=b;
by cc;
run;


data last_second;
  set b;
  by cc;
last_second_value = lag(swipe);
if last.cc then do;
    output;
  end;
  drop swipe;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&amp;nbsp;How to get last second values using PROC SQL Method &lt;BR /&gt;output &lt;BR /&gt;cc swipe&lt;BR /&gt;1 200&lt;BR /&gt;2 1400&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Jul 2024 09:52:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-second-value-for-each-group/m-p/935890#M367915</guid>
      <dc:creator>pavank</dc:creator>
      <dc:date>2024-07-16T09:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: last second value for each group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/last-second-value-for-each-group/m-p/935895#M367916</link>
      <description>&lt;P&gt;SQL does not have a concept of sequence, it works with sets. Stay with the DATA step, e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data last_second;
do until (last.cc);
  set b;
  by cc;
  if not last.cc then last_second_value = swipe;
end;
drop swipe;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your code can be simplified by using a Subsetting IF:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data last_second;
set b;
by cc;
last_second_value = lag(swipe);
if last.cc and not first.cc;
drop swipe;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The expanded condition takes care of cases where there's only one observation for a cc group.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Jul 2024 09:59:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/last-second-value-for-each-group/m-p/935895#M367916</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-07-16T09:59:50Z</dc:date>
    </item>
  </channel>
</rss>

