<?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 to get the latest and second latest value in a group? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735517#M38606</link>
    <description>&lt;P&gt;To avoid any re-sorting of the table, run a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
counta = 0;
do until (last.type);
  set have;
  by type;
  counta + 1;
end;
countb = 0;
do until (last.type);
  set have;
  by type;
  countb + 1;
  if countb = counta - 1 then second_last_color = color;
  if countb = count then last_color = color;
end;
keep type last_color second_last_color;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can merge this table to the transposed one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Apr 2021 12:42:32 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-04-20T12:42:32Z</dc:date>
    <item>
      <title>How to get the latest and second latest value in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735227#M38581</link>
      <description>&lt;P&gt;Im trying to add 2 columns to &lt;STRONG&gt;want_wide&lt;/STRONG&gt; dataset.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I'm trying to add the column &lt;STRONG&gt;last_color&lt;/STRONG&gt; and &lt;STRONG&gt;second_last_color&lt;/STRONG&gt; for every type. &lt;STRONG&gt;last_color&lt;/STRONG&gt; is the color which is mentioned in the last quarter (so max quarter). &lt;STRONG&gt;second_last_color &lt;/STRONG&gt;is the color which is mentioned in the &lt;STRONG&gt;second_last_quarter&lt;/STRONG&gt; (so one before the max)&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;STRONG&gt;This is my current code:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input type $ quarter phase colors $ creation_date :yymmdd6.;
  format creation_date yymmdd6.;
  datalines;
K-11 202101 1 blue 20200101
K-11 202102 2 blue 20200101
K-11 202003 3 blue 20200101
K-11 202104 3 black 20200101
K-12 202101 3 blue 20200101
K-12 202102 1 white 20200101
K-12 202103 1 white 20200101
K-12 202104 1 yellow 20200101
K-13 202003 2 green 20200101
K-13 202101 3 green 20200101
K-13 202102 2 red 20200101
K-13 202103 2 blue 20200101
;

proc sort data=have;
by type quarter;
run;

data want;
set have;
where phase ne .;
by type;
retain diff_phase;
if first.type then diff_phase = phase;
if last.type
then do;
  diff_phase = phase - diff_phase;
  output;
end;
diff_phase = phase;
keep type diff_phase;
run;

proc sort data=have;
by type quarter;
run;

data long;
set
  have
;
run;

proc transpose
  data=long
  out=wide (
    drop=_name_
    where=(type ne "")
  )
  prefix=q_
;
by type;
var phase;
id quarter;
run;


data want_wide;
merge
  wide
  want
;
by type;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is how &lt;STRONG&gt;want_wide&amp;nbsp;&lt;/STRONG&gt;looks like right now:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;type&amp;nbsp; &amp;nbsp; q_202003&amp;nbsp;&amp;nbsp;   q_202101&amp;nbsp;        q_202102&amp;nbsp;       q_202104&amp;nbsp;      q_202103&amp;nbsp;       diff_phase&amp;nbsp;&amp;nbsp;
K-11&amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;  3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-1
K-12&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0
K-13&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is how I want &lt;STRONG&gt;want_wide&lt;/STRONG&gt; to look:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;type&amp;nbsp; &amp;nbsp; q_202003&amp;nbsp;&amp;nbsp;   q_202101&amp;nbsp;        q_202102&amp;nbsp;       q_202104&amp;nbsp;      q_202103&amp;nbsp;       diff_phase&amp;nbsp;&amp;nbsp;last_color   second_last_color date
K-11&amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;  3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-1          black        blue              20200101
K-12&amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0          yellow       white             20200101
K-13&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;     blue         red&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;20200101&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How can I achieve that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 13:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735227#M38581</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-19T13:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the latest and second latest value in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735229#M38582</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input type $ quarter phase colors $ creation_date :yymmdd6.;
  format creation_date yymmdd6.;
  datalines;
K-11 202101 1 blue 20200101
K-11 202102 2 blue 20200101
K-11 202003 3 blue 20200101
K-11 202104 3 black 20200101
K-12 202101 3 blue 20200101
K-12 202102 1 white 20200101
K-12 202103 1 white 20200101
K-12 202104 1 yellow 20200101
K-13 202003 2 green 20200101
K-13 202101 3 green 20200101
K-13 202102 2 red 20200101
K-13 202103 2 blue 20200101
;

proc sort data=have;
by type quarter;
run;

data long;
set
  have
;
run;

proc transpose
  data=long
  out=wide (
    drop=_name_
    where=(type ne "")
  )
  prefix=q_
;
by type;
var phase;
id quarter;
run;



data temp;
merge have have(firstobs=3 keep=type rename=(type=_type));
if type ne _type;
drop _type;
run;
data temp;
 set temp;
 by type;
 if first.type then n=0;
 n+1;
 if n=1 then id='second_last_color';
  else id='last_color             ';
proc transpose data=temp out=want(drop=_name_);
by type ;
id id;
var colors;
run;





data want_wide;
merge
  wide
  want
;
by type;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Apr 2021 12:33:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735229#M38582</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-19T12:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the latest and second latest value in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735237#M38583</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;STRONG&gt;want_wide&lt;/STRONG&gt; looks good but I miss 2 columns, &lt;STRONG&gt;diff_phase &lt;/STRONG&gt;and &lt;STRONG&gt;creation date &lt;/STRONG&gt;are not&amp;nbsp;in &lt;STRONG&gt;want_wide&lt;/STRONG&gt; anymore. Why is that?&lt;/P&gt;</description>
      <pubDate>Mon, 19 Apr 2021 13:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735237#M38583</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-19T13:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the latest and second latest value in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735501#M38601</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input type $ quarter phase colors $ creation_date :yymmdd6.;
  format creation_date yymmdd6.;
  datalines;
K-11 202101 1 blue 20200101
K-11 202102 2 blue 20200101
K-11 202003 3 blue 20200101
K-11 202104 3 black 20200101
K-12 202101 3 blue 20200101
K-12 202102 1 white 20200101
K-12 202103 1 white 20200101
K-12 202104 1 yellow 20200101
K-13 202003 2 green 20200101
K-13 202101 3 green 20200101
K-13 202102 2 red 20200101
K-13 202103 2 blue 20200101
;

proc sort data=have;
by type quarter;
run;

data long;
set
  have
;
run;

proc transpose
  data=long
  out=wide (
    drop=_name_
    where=(type ne "")
  )
  prefix=q_
;
by type;
var phase;
id quarter;
run;



data temp;
merge have have(firstobs=3 keep=type rename=(type=_type));
if type ne _type;
drop _type;
run;
data temp;
 set temp;
 by type;
 if first.type then n=0;
 n+1;
 if n=1 then id='second_last';
  else id='last      ';
run;
proc transpose data=temp out=want1(drop=_name_) suffix=_color;
by type creation_date;
id id;
var colors ;
run;
proc transpose data=temp out=want2(drop=_name_) suffix=_phase;
by type creation_date;
id id;
var phase ;
run;
data want;
 merge want1 want2;
by type creation_date;
run;



data want_wide;
merge
  wide
  want
;
by type;
diff=second_last_phase-last_phase;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Apr 2021 12:12:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735501#M38601</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-20T12:12:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the latest and second latest value in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735517#M38606</link>
      <description>&lt;P&gt;To avoid any re-sorting of the table, run a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
counta = 0;
do until (last.type);
  set have;
  by type;
  counta + 1;
end;
countb = 0;
do until (last.type);
  set have;
  by type;
  countb + 1;
  if countb = counta - 1 then second_last_color = color;
  if countb = count then last_color = color;
end;
keep type last_color second_last_color;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can merge this table to the transposed one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Apr 2021 12:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735517#M38606</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-20T12:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the latest and second latest value in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735827#M38631</link>
      <description>&lt;P&gt;I think the double DO is a bit overkill in this particular case.&amp;nbsp; A lag function does all that is needed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (keep=type colors rename=(colors=last_color));
  by type;
  second_last_color=lag(last_color);
  if last.type;
  /*if first.type=1 then call missing(second_last_color);*/
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;De-comment the "if first.type=1" statement if you expect any TYPE with only one observation, thereby avoiding an erroneous second_last_color.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;suggested, merge it with the transposed table.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 05:19:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735827#M38631</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-04-21T05:19:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the latest and second latest value in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735881#M38636</link>
      <description>I ran your snippet and merged it with the transposed table, but I miss 2 essential columns that I had in my HAVE dataset. I miss diff_phase and ceation_date.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Apr 2021 08:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-latest-and-second-latest-value-in-a-group/m-p/735881#M38636</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-04-21T08:56:32Z</dc:date>
    </item>
  </channel>
</rss>

