<?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: Get the highest value within a window in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919142#M362046</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49486"&gt;@hhchenfx&lt;/a&gt;: Have you changed the requirements? I'm asking because &lt;EM&gt;four&lt;/EM&gt; solutions were suggested, only&amp;nbsp;&lt;EM&gt;three&lt;/EM&gt; of which, applied to your sample data, produce the results in your WANT dataset (in the case of Kurt Bremser's code after adapting the &lt;FONT face="courier new,courier"&gt;start&lt;/FONT&gt; date), but you have accepted the one that ignores gaps in the sequence of dates.&lt;/P&gt;</description>
    <pubDate>Wed, 06 Mar 2024 16:02:00 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2024-03-06T16:02:00Z</dc:date>
    <item>
      <title>Get the highest value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919034#M361997</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;So I have a data with ID, date (not continuous) and value.&lt;/P&gt;
&lt;P&gt;For each row, I create a look back, which is 4 date before.&lt;/P&gt;
&lt;P&gt;I want to find the max value within that 4 date (excluding current date)&lt;/P&gt;
&lt;P&gt;My code below gives me the result but I need to run a sort nodupkey.&lt;/P&gt;
&lt;P&gt;Can you please help me fix it?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id date value;
datalines;
1 1 3
1 4 7
1 15 3
1 25 20
1 26 8
1 27 15
1 28 5
1 29 5
1 30 5
1 31 6
;
run;

data have; set have;
lookback_date=date - 4;
run;

proc sql;
create table want as select a.*, max(b.value) as max from have as a left join have as b 
on a.id=b.id and a.lookback_date&amp;lt;=b.date and a.date&amp;gt;b.date
group by a.id, a.date;
quit;

proc sort data =want out=want nodupkey; by id date; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 05:34:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919034#M361997</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2024-03-06T05:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Get the highest value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919035#M361998</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*If you don't have a big table, you could try this sub-query.*/

data have;
input id date value;
datalines;
1 1 3
1 4 7
1 15 3
1 25 20
1 26 8
1 27 15
1 28 5
1 29 5
1 30 5
1 31 6
;
run;


proc sql;
create table want2 as
select *,(select max(value) from have where id=a.id and a.date-4&amp;lt;=date&amp;lt;a.date) as max
 from have as a;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Mar 2024 05:46:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919035#M361998</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-03-06T05:46:48Z</dc:date>
    </item>
    <item>
      <title>Re: Get the highest value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919050#M362005</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49486"&gt;@hhchenfx&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a DATA step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let w=4; /* length of time window (in days) */

data want(drop=i j);
set have;
by id date;
array d[0:&amp;amp;w] _temporary_; /* current and last four dates */
array v[0:&amp;amp;w] _temporary_; /* current and last four values */
if first.id then call missing(of d[*], of v[*]);
d[mod(_n_,&amp;amp;w+1)]=date;
v[mod(_n_,&amp;amp;w+1)]=value;
max=.;
do i=1 to &amp;amp;w;
  j=mod(_n_+&amp;amp;w+1-i,&amp;amp;w+1);
  if date-&amp;amp;w&amp;gt;d[j] then leave;
  if v[j]&amp;gt;max then max=v[j];
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It assumes that the input dataset (HAVE) is sorted by ID DATE and that there are no duplicate dates within an ID.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 10:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919050#M362005</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-03-06T10:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Get the highest value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919071#M362016</link>
      <description>&lt;P&gt;Is this returning what you're after?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id date;
  array values{0:3} 8 _temporary_;
  if first.id then call missing(of values[*]);
  else max_val=max(of values[*]);
  values[mod(_n_,4)]=value;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1709723700507.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94409i4DDF3EE359EA09BF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1709723700507.png" alt="Patrick_0-1709723700507.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 11:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919071#M362016</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-03-06T11:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: Get the highest value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919074#M362018</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start = %sysfunc(inputn(2010-01-01,yymmdd10.));
%let end = %sysfunc(inputn(2024-12-31,yymmdd10.));

data want;
set have;
by id;
array v{&amp;amp;start.:&amp;amp;end.} _temporary_;
if first.id
then do i = &amp;amp;start. to &amp;amp;end.;
  v{i} = .;
end;
v{date} = value;
max = max(v{date-4},v{date-3},v{date-2},v{date-1});
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Always store dates as SAS dates!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 11:52:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919074#M362018</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-03-06T11:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: Get the highest value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919142#M362046</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/49486"&gt;@hhchenfx&lt;/a&gt;: Have you changed the requirements? I'm asking because &lt;EM&gt;four&lt;/EM&gt; solutions were suggested, only&amp;nbsp;&lt;EM&gt;three&lt;/EM&gt; of which, applied to your sample data, produce the results in your WANT dataset (in the case of Kurt Bremser's code after adapting the &lt;FONT face="courier new,courier"&gt;start&lt;/FONT&gt; date), but you have accepted the one that ignores gaps in the sequence of dates.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 16:02:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-the-highest-value-within-a-window/m-p/919142#M362046</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-03-06T16:02:00Z</dc:date>
    </item>
  </channel>
</rss>

