<?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: previous max and succeeding max of n sample in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358449#M84227</link>
    <description>No alas not. Btw I based this code on some moving average code, which I believe you created (-:</description>
    <pubDate>Sat, 13 May 2017 16:24:32 GMT</pubDate>
    <dc:creator>csetzkorn</dc:creator>
    <dc:date>2017-05-13T16:24:32Z</dc:date>
    <item>
      <title>previous max and succeeding max of n sample</title>
      <link>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358444#M84224</link>
      <description>&lt;P&gt;I came up with the code below, which intends to create 2 columns:&amp;nbsp;MaxPrev and MaxSucc which contain the maximum value of the previous and succeeding 3 sample (based on&amp;nbsp;DateId). There are 2 columns which group the samples:&amp;nbsp;ID1 ID2. The code seems to work fine but maybe it can be improved. Any suggestions would be very much appreciated. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA work.have;
   INPUT ID1 ID2 DateId	SomeValue;
   DATALINES;
	1 2 1 10
	1 2 2 10
	1 2 3 10
	1 2 4 10
	1 2 5 20
	1 2 6 10
	1 2 7 10
	1 2 8 10
	1 2 9 10
	1 2 10 10
	1 2 11 10
	1 2 12 10
	1 2 13 10
	1 2 14 10
	1 2 15 10
	1 2 16 10
	1 2 17 10
	1 2 18 10
	1 2 19 10
	1 2 20 10
	1 2 21 10
	1 2 22 10
	1 2 23 10
	1 2 24 30
	1 2 25 10
	1 2 26 10
	1 2 27 10
	1 2 28 10
	1 2 29 10
	1 2 30 10
	1 2 31 10
	1 2 32 10
	1 2 33 10
run;

%let winsize=4;

proc sort data = have;
 	by ID1 ID2 DateId ;
run; 

data have;
	set have;
	by ID1 ID2;

	retain pre1-pre&amp;amp;winsize.;
	array pre(&amp;amp;winsize.);

	if first.ID1 then
		do;
			call missing(of pre(*));
			count=0;
		end;
	count+1;
	index=mod(count, &amp;amp;winsize.)+1;
	pre(index)=SomeValue;

	if count&amp;gt;=&amp;amp;winsize. then
		MaxPrev=max(of pre(*));
	drop count index pre1-pre&amp;amp;winsize.;
run;

proc sort data = have;
 	by ID1 ID2 descending DateId ;
run; 

data have;
	set have;
	by ID1 ID2;
	
	retain pre1-pre&amp;amp;winsize.;
	array pre(&amp;amp;winsize.);

	if first.ID1 then
		do;
			call missing(of pre(*));
			count=0;
		end;
	count+1;
	index=mod(count, &amp;amp;winsize.)+1;
	pre(index)=SomeValue;

	if count&amp;gt;=&amp;amp;winsize. then
		MaxSucc=max(of pre(*));
	drop count index pre1-pre&amp;amp;winsize.;
run;

proc sort data = have;
 	by ID1 ID2 DateId ;
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 May 2017 15:34:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358444#M84224</guid>
      <dc:creator>csetzkorn</dc:creator>
      <dc:date>2017-05-13T15:34:49Z</dc:date>
    </item>
    <item>
      <title>Re: previous max and succeeding max of n sample</title>
      <link>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358448#M84226</link>
      <description>&lt;P&gt;Do you have SAS/ETS licensed?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so look at PROC EXPAND.&lt;/P&gt;</description>
      <pubDate>Sat, 13 May 2017 16:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358448#M84226</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-05-13T16:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: previous max and succeeding max of n sample</title>
      <link>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358449#M84227</link>
      <description>No alas not. Btw I based this code on some moving average code, which I believe you created (-:</description>
      <pubDate>Sat, 13 May 2017 16:24:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358449#M84227</guid>
      <dc:creator>csetzkorn</dc:creator>
      <dc:date>2017-05-13T16:24:32Z</dc:date>
    </item>
    <item>
      <title>Re: previous max and succeeding max of n sample</title>
      <link>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358453#M84229</link>
      <description>&lt;P&gt;That's fine, I wasn't using it &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Most of my code originates from somewhere else originally.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If window size is less than 5 it may be easier to just use the LAG functions. In combination with a short cut list, ie max(of age_lag:) you may be able to skip some steps.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did also write this macro which will create the lagged variables required.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/cb52f1a54868e959a838f2b6f3dfad20" target="_blank"&gt;https://gist.github.com/statgeek/cb52f1a54868e959a838f2b6f3dfad20&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 May 2017 17:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/previous-max-and-succeeding-max-of-n-sample/m-p/358453#M84229</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-05-13T17:24:25Z</dc:date>
    </item>
  </channel>
</rss>

