<?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 find the greatest value among the last N rows in data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827025#M326675</link>
    <description>&lt;P&gt;I see a logic to generate the PICK dummy, but the logic for COUNT escapes me:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if value^=. and value&amp;gt;lag(value) and value&amp;gt;lag2(value) then pick=1;
  else pick=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 03 Aug 2022 23:21:09 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-08-03T23:21:09Z</dc:date>
    <item>
      <title>How to find the greatest value among the last N rows in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827001#M326667</link>
      <description>&lt;P&gt;I want to mark a row if it has the greatest non-missing value among the last N rows. Below is an example where N=3:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
 	infile datalines delimiter = "," missover;
	input value;
	datalines;
	.
	.
	1.5
	.
	.
	.
	.
	1.1
	.
	2.0
	.
	.
	.
	2.2
	1.7
	.
	.
	.
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
 	infile datalines delimiter = "," missover;
	input value mark count;
	datalines;
	., 0, .
	., 0, .
	1.5, 1, 1
	., 0, 2
	., 0, 3
	., 0, .
	., 0, .
	1.1, 1, 1
	., 0, 2
	2.0, 1, 1
	., 0, 2
	., 0, 3
	., 0, .
	2.2, 1, 1
	1.7, 0, 2
	., 0, 3
	, 0, .
	., 0,.
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I attempted to do this with retain command but didn't get what I want:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data get;&lt;BR /&gt;set have;&lt;BR /&gt;mark = 0;&lt;BR /&gt;retain value_old;&lt;BR /&gt;if value ne . and value &amp;gt; value_old then do;&lt;BR /&gt;mark = 1;&lt;BR /&gt;count = 1;&lt;BR /&gt;value_old = value;&lt;BR /&gt;end;&lt;BR /&gt;if lag(count) &amp;gt; 0 then count = lag(count) + 1;&lt;BR /&gt;if (value = . or value &amp;lt; value_old) and lag(count) = 3 then do;&lt;BR /&gt;count = .;&lt;BR /&gt;value_old = .;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 22:51:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827001#M326667</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2022-08-03T22:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the greatest value among the last N rows in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827006#M326668</link>
      <description>&lt;P&gt;I don't see how you get that output from your definition. I think you mean something different that what you said.&lt;/P&gt;
&lt;P&gt;Here is what it sounds like you asked for:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input value @@;
datalines;
. . 1.5 . . . . 1.1 . 2 . . . 2.2 1.7 . .
;

data max_over_last_3;
  set have;
  array last3 [0:2] _temporary_;
  last3[mod(_n_,3)] = value;
  max = max(of last3[*]);
  n = n(of last3[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    value    max    n

  1      .       .     0
  2      .       .     0
  3     1.5     1.5    1
  4      .      1.5    1
  5      .      1.5    1
  6      .       .     0
  7      .       .     0
  8     1.1     1.1    1
  9      .      1.1    1
 10     2.0     2.0    2
 11      .      2.0    1
 12      .      2.0    1
 13      .       .     0
 14     2.2     2.2    1
 15     1.7     2.2    2
 16      .      2.2    2
 17      .      1.7    1
&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 21:01:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827006#M326668</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-03T21:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the greatest value among the last N rows in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827025#M326675</link>
      <description>&lt;P&gt;I see a logic to generate the PICK dummy, but the logic for COUNT escapes me:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if value^=. and value&amp;gt;lag(value) and value&amp;gt;lag2(value) then pick=1;
  else pick=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 23:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827025#M326675</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-03T23:21:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the greatest value among the last N rows in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827159#M326721</link>
      <description>&lt;P&gt;Good day&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Half the answer was already provided by a previous user, I'm just adding the last bit.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want_pick;
  set have;
  if value^=. and value&amp;gt;lag(value) and value&amp;gt;lag2(value) then pick=1;
  else pick=0;
run;

%let N = 3;

data want_count (rename=count_clean=count drop=count);
	set want_pick;
	retain count;

	if pick = 1 then
		count=1;
	else if lag(count)&amp;gt;0 then
		count+1;

	if count&amp;lt;=&amp;amp;N. then
		count_clean = count;
	else count_clean=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Aug 2022 16:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-the-greatest-value-among-the-last-N-rows-in-data/m-p/827159#M326721</guid>
      <dc:creator>Adriaan_Gouws</dc:creator>
      <dc:date>2022-08-04T16:03:52Z</dc:date>
    </item>
  </channel>
</rss>

