<?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 last 8 values in a group? in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779085#M39826</link>
    <description>&lt;P&gt;something like this?&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 201904 1 blue 20200101
K-11 202001 1 blue 20200101
K-11 202002 2 blue 20200101
K-11 202003 3 blue 20200101
K-11 202004 3 black 20200101
K-11 202101 1 blue 20200101
K-11 202102 2 blue 20200101
K-11 202103 3 blue 20200101
K-11 202104 3 black 20200101
K-15 202101 3 green 20200101
K-15 202102 2 red 20200101
K-15 202103 2 blue 20200101
K-15 202104 2 blue 20200101
K-12 201904 3 blue 20200101
K-12 202001 3 blue 20200101
K-12 202002 1 white 20200101
K-12 202003 1 white 20200101
K-12 202004 1 yellow 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 201904 2 green 20200101
K-13 202001 3 green 20200101
K-13 202002 2 red 20200101
K-13 202003 2 blue 20200101
K-13 202004 2 green 20200101
K-13 202101 3 green 20200101
K-13 202102 2 red 20200101
K-13 202103 2 blue 20200101
K-13 202104 2 blue 20200101
K-14 202101 3 green 20200101
K-14 202102 2 red 20200101
K-14 202103 2 blue 20200101
K-14 202104 2 blue 20200101
;
run;

%let lookback = 8;

data want (rename=(lag_type=type));

  do point = nobs to 1 by -1, nobs;
    set have point = point nobs=nobs;
    lag_type = lag(type);

    array color_last_[&amp;amp;lookback.] $ 12;

    if lag_type ne type then 
      do;  
        if lag_type ne " " then output;
        _N_ = 1;
        call missing(of color_last_{*});
      end;

    if _N_ &amp;lt;= &amp;amp;lookback. then color_last_[_N_] = colors;
    _N_ + 1;
  end;

stop;
keep lag_type color_last_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;B.&lt;/P&gt;</description>
    <pubDate>Mon, 08 Nov 2021 09:59:51 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2021-11-08T09:59:51Z</dc:date>
    <item>
      <title>How to get the last 8 values in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779070#M39825</link>
      <description>&lt;P&gt;I have two datasets `HAVE` and `HAVE2`. I'm trying to add the column&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;last_color,&amp;nbsp;&lt;/STRONG&gt;&lt;STRONG&gt;second_last_color, third_last_color etc. until eight_last_color&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;for every type.&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;last_color&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;is the color which is mentioned in the last quarter (so max quarter).&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;second_last_color&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/STRONG&gt;is the color which is mentioned in the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;second_last_quarter&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;(so one before the max) etc. Now in my current code I was only able to calculate the last and &lt;STRONG&gt;second_last_color&lt;/STRONG&gt;, I dont know how to calculate until the &lt;STRONG&gt;eight_last_color&lt;/STRONG&gt;. But I think I'm very close...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;These are my 2 datasets:&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 201904 1 blue 20200101
K-11 202001 1 blue 20200101
K-11 202002 2 blue 20200101
K-11 202003 3 blue 20200101
K-11 202004 3 black 20200101
K-11 202101 1 blue 20200101
K-11 202102 2 blue 20200101
K-11 202103 3 blue 20200101
K-11 202104 3 black 20200101
K-12 201904 3 blue 20200101
K-12 202001 3 blue 20200101
K-12 202002 1 white 20200101
K-12 202003 1 white 20200101
K-12 202004 1 yellow 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 201904 2 green 20200101
K-13 202001 3 green 20200101
K-13 202002 2 red 20200101
K-13 202003 2 blue 20200101
K-13 202004 2 green 20200101
K-13 202101 3 green 20200101
K-13 202102 2 red 20200101
K-13 202103 2 blue 20200101
K-13 202104 2 blue 20200101
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
  infile datalines truncover;
  input type $ quarter;
  datalines;
K-11 -1
K-12 2
K-13 3
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;This is my code:&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long;
set
  work.HAVE
;
run;
proc transpose
  data=long
  out=wide (
    drop=_name_
    where=(type ne "")
  )
  prefix=q_
;
by type;
var phase;
id quarter;
run;
data calc_yearquarter;
merge
  wide
  work.have2
;
by type;
run;
data temp;
merge work.HAVE work.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_advice';
  else id='last_advice';
run;
proc transpose data=temp out=calc_advice(drop=_name_);
by type ;
id id;
var colors;
run;
data SAS11
;
merge
  calc_yearquarter
  calc_advice
;
by type;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So to conclude, right now I have calculated 2 quarters back. I want to calculate 8 quarters back...&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 08:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779070#M39825</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-11-08T08:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last 8 values in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779085#M39826</link>
      <description>&lt;P&gt;something like this?&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 201904 1 blue 20200101
K-11 202001 1 blue 20200101
K-11 202002 2 blue 20200101
K-11 202003 3 blue 20200101
K-11 202004 3 black 20200101
K-11 202101 1 blue 20200101
K-11 202102 2 blue 20200101
K-11 202103 3 blue 20200101
K-11 202104 3 black 20200101
K-15 202101 3 green 20200101
K-15 202102 2 red 20200101
K-15 202103 2 blue 20200101
K-15 202104 2 blue 20200101
K-12 201904 3 blue 20200101
K-12 202001 3 blue 20200101
K-12 202002 1 white 20200101
K-12 202003 1 white 20200101
K-12 202004 1 yellow 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 201904 2 green 20200101
K-13 202001 3 green 20200101
K-13 202002 2 red 20200101
K-13 202003 2 blue 20200101
K-13 202004 2 green 20200101
K-13 202101 3 green 20200101
K-13 202102 2 red 20200101
K-13 202103 2 blue 20200101
K-13 202104 2 blue 20200101
K-14 202101 3 green 20200101
K-14 202102 2 red 20200101
K-14 202103 2 blue 20200101
K-14 202104 2 blue 20200101
;
run;

%let lookback = 8;

data want (rename=(lag_type=type));

  do point = nobs to 1 by -1, nobs;
    set have point = point nobs=nobs;
    lag_type = lag(type);

    array color_last_[&amp;amp;lookback.] $ 12;

    if lag_type ne type then 
      do;  
        if lag_type ne " " then output;
        _N_ = 1;
        call missing(of color_last_{*});
      end;

    if _N_ &amp;lt;= &amp;amp;lookback. then color_last_[_N_] = colors;
    _N_ + 1;
  end;

stop;
keep lag_type color_last_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;B.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 09:59:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779085#M39826</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-11-08T09:59:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last 8 values in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779086#M39827</link>
      <description>&lt;P&gt;But how to do it in combination with my existing code?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 10:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779086#M39827</guid>
      <dc:creator>Andalusia</dc:creator>
      <dc:date>2021-11-08T10:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last 8 values in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779104#M39831</link>
      <description>&lt;P&gt;what is the purpose of have2 data set?&lt;/P&gt;
&lt;P&gt;what final result are you expecting?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 12:16:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779104#M39831</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-11-08T12:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the last 8 values in a group?</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779937#M39851</link>
      <description>&lt;P&gt;I suck at code but if I interpret your question correctly you can sort your data and create a new column using a 'first' function and count the rows within a specific group...&lt;BR /&gt;&lt;BR /&gt;sort your data descending (last color on top)&lt;/P&gt;&lt;P&gt;count per group with a rank number +1 for every new instance until you encounter the next group (The K-11,K-12 in your data set I think?)&lt;BR /&gt;&lt;BR /&gt;And then only filter the rows labelled 1 till 8 and reverse them again if needed.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&amp;nbsp;"have";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET "output tabel";&lt;/P&gt;&lt;P&gt;by "K-11 colunm name";&lt;/P&gt;&lt;P&gt;RANK +&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first."K-11 colum name" then RANK = &lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;;&lt;BR /&gt;&lt;BR /&gt;first sort it the right way, and maybe add other column to your BY group when needed.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Nov 2021 08:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-last-8-values-in-a-group/m-p/779937#M39851</guid>
      <dc:creator>Pepijn</dc:creator>
      <dc:date>2021-11-12T08:51:47Z</dc:date>
    </item>
  </channel>
</rss>

