<?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 Can proc expand return the ID / position where it found the max value of movmax? in SAS Forecasting and Econometrics</title>
    <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836044#M4521</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date id value ;
datalines;
1 1 1
2 1 9
3 1 0
4 1 7
5 1 5
1 2 100
2 2 0
3 2 -2
4 2 2
5 2 0
6 2 1
;

proc expand data=have out=outy method=none;
by id;
   id date;
   convert value = max_value   / transform=(movmax 3) ;
run;

data have1;
set have;
by id value date notsorted;
if first.value;
run;

proc sql;
create table want as
select a.*, b.date as earliest_max_date from 
outy a left join have1 b on
a.max_value=b.value and a.id=b.id
order by id, date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 30 Sep 2022 11:40:36 GMT</pubDate>
    <dc:creator>acordes</dc:creator>
    <dc:date>2022-09-30T11:40:36Z</dc:date>
    <item>
      <title>Can proc expand return the ID / position where it found the max value of movmax?</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836044#M4521</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date id value ;
datalines;
1 1 1
2 1 9
3 1 0
4 1 7
5 1 5
1 2 100
2 2 0
3 2 -2
4 2 2
5 2 0
6 2 1
;

proc expand data=have out=outy method=none;
by id;
   id date;
   convert value = max_value   / transform=(movmax 3) ;
run;

data have1;
set have;
by id value date notsorted;
if first.value;
run;

proc sql;
create table want as
select a.*, b.date as earliest_max_date from 
outy a left join have1 b on
a.max_value=b.value and a.id=b.id
order by id, date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Sep 2022 11:40:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836044#M4521</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2022-09-30T11:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: Can proc expand return the ID / position where it found the max value of movmax?</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836315#M4523</link>
      <description>&lt;P&gt;I'm unaware of proc expand being able to extract an identifier for within-window observations having a particular value statistic.&amp;nbsp; But you can do this in a fairly simple data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  by id;

  array value_window {0:2} _temporary_;
  array date_window  {0:2} _temporary_;

  if first.id then call missing(of value_window{*},of date_window{*});
  _i=mod(_n_,3);
  value_window{_i}=value;
  date_window{_i}=date;

  max_value=max(of value_window{*});
  do _n=max(1,_n_-2) to _n_ until (value_window{_i}=max_value);
    _i=mod(_n,3);
    earliest_max_date=date_window{_i};
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the two arrays are NOT usually (i.e. two-thirds of the time) in date order.&amp;nbsp; But that doesn't matter, because you won't process the arrays in index order.&amp;nbsp; Instead they are processed in observation order which in turn is mapped to array index _i.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also: the do statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  do _n=max(1,_n_-2) to _n_ until (value_window{_i}=max_value);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is used instead of&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  do _n=_n_-2 to _n_ until (value_window{_i}=max_value);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;because _n-2 is negative and will generate negative values of the index _i for the first two observations.&amp;nbsp; And _n-1 will similarly generate a negative index for one observation.&amp;nbsp; Those negative indexes will not be valid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited note:&amp;nbsp; The code sequence&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  do _n=max(1,_n_-2) to _n_ until (value_window{_i}=max_value);
    _i=mod(_n,3);
    earliest_max_date=date_window{_i};
  end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;might be better put as&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  do _n=max(1,_n_-2) to _n_ until (value_window{_i}=max_value);
    _i=mod(_n,3);
  end;
  earliest_max_date=date_window{_i};&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The results are the same, but the second version makes it more obvious that there is no need to repeatedly assign a value to &lt;EM&gt;&lt;STRONG&gt;earliest_max_date&lt;/STRONG&gt;&lt;/EM&gt; inside the loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Oct 2022 15:08:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836315#M4523</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-10-02T15:08:56Z</dc:date>
    </item>
    <item>
      <title>Re: Can proc expand return the ID / position where it found the max value of movmax?</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836379#M4524</link>
      <description>So arrays used that way intrinsically retain the value, right?</description>
      <pubDate>Sun, 02 Oct 2022 13:03:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836379#M4524</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2022-10-02T13:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: Can proc expand return the ID / position where it found the max value of movmax?</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836385#M4525</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/127222"&gt;@acordes&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;So arrays used that way intrinsically retain the value, right?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It is not how they are used, but how they are defined.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Temporary arrays are retained.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you define the array to use actual data step variables then whether or not to retain is determined separately for each variable.&amp;nbsp; The same as variables that are not made available to be referenced via an array.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Oct 2022 14:15:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Can-proc-expand-return-the-ID-position-where-it-found-the-max/m-p/836385#M4525</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-02T14:15:00Z</dc:date>
    </item>
  </channel>
</rss>

