<?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: find maximum in time series with arrey loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269772#M53488</link>
    <description>works perfect with lag for the previous obs! yeah!</description>
    <pubDate>Wed, 11 May 2016 15:56:11 GMT</pubDate>
    <dc:creator>Sandra_L</dc:creator>
    <dc:date>2016-05-11T15:56:11Z</dc:date>
    <item>
      <title>find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269472#M53387</link>
      <description>&lt;P&gt;Hi folks!&lt;/P&gt;&lt;P&gt;I have a time series of precipitation data for 30 years (jahre) for a lot of stations (idgem). Each year has a value for a half month step like this (jan1,..., dec2)&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/3111i1367D3B0B5A91C0E/image-size/original?v=v2&amp;amp;px=-1" alt="bsp.GIF" title="bsp.GIF" border="0" /&gt;&lt;/P&gt;&lt;P&gt;Now I want to find out the local maxima in the way that the values are kept, if they are bigger than the previous and the next. I tried it this way&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test3;
 set test2;
  array maxi [24] jan1 jan2 feb1 feb2 mar1 mar2 apr1 apr2 may1 may2 jun1 jun2 
                  jul1 jul2 aug1 aug2 sep1 sep2 oct1 oct2 nov1 nov2 dec1 dec2;
   do i=1 to 24;
   if maxi [i-1] &amp;lt; maxi [i]&amp;gt; maxi [i+1] then maxi [i] = maxi [i];
   else maxi [i]=.;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I always get an error message "Array subscript out of range ".&lt;/P&gt;&lt;P&gt;Any ideas how I can tell SAS to compare wiht the previous and next value and keep the compared value if it is bigger? And how do I compare the value for example of jan1 of 1961 to dec2 of 1960?&lt;/P&gt;&lt;P&gt;Thanks a lot,&lt;/P&gt;&lt;P&gt;Sandra&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2016 15:10:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269472#M53387</guid>
      <dc:creator>Sandra_L</dc:creator>
      <dc:date>2016-05-10T15:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269474#M53389</link>
      <description>Its not necessary to do an array, you could use the max function.&lt;BR /&gt;max(jan1,jan2,feb1,..);</description>
      <pubDate>Tue, 10 May 2016 15:14:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269474#M53389</guid>
      <dc:creator>arodriguez</dc:creator>
      <dc:date>2016-05-10T15:14:00Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269477#M53391</link>
      <description>&lt;P&gt;If you are trying to find a maximum Value then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;maxValue = max(of Maxi(*));&lt;/P&gt;
&lt;P&gt;If you want to know the first occurence of that maximum:&lt;/P&gt;
&lt;P&gt;MaxPos = whichn(MaxValue,of Maxi(*));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LAG is the traditional function for examining values of variables on previous observations of a data set.&lt;/P&gt;
&lt;P&gt;You would have to provide more details as to what you are attempting with the look back to get a more complete example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lag(Dec2) would give you the value of Dec2 from the previous record though there are some issues about the presence of Lag in IF constructs.&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2016 15:18:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269477#M53391</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-05-10T15:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269481#M53395</link>
      <description>&lt;P&gt;So it sounds like you want to keep multiple local maxima.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do you want to handle JAN1 &amp;gt; JAN2?&amp;nbsp; Would JAN1 be a local maximum?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What about DEC2 when DEC1 &amp;lt; DEC2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2016 15:25:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269481#M53395</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-05-10T15:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269482#M53396</link>
      <description>&lt;P&gt;Hey,&lt;/P&gt;&lt;P&gt;but the max function gives me just one maximal value. Whar´t i want are local maxima, compared to the previous and next value&lt;/P&gt;&lt;P&gt;For example as input:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; jan1&amp;nbsp;&amp;nbsp; jan2&amp;nbsp;&amp;nbsp;&amp;nbsp; feb1&amp;nbsp;&amp;nbsp; feb2&amp;nbsp;&amp;nbsp;&amp;nbsp; mar1&amp;nbsp;&amp;nbsp;&amp;nbsp; mar2&amp;nbsp;&amp;nbsp;&amp;nbsp; apr1&amp;nbsp;&amp;nbsp;&amp;nbsp; apr1&lt;/P&gt;&lt;P&gt;1960&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 19&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;as output i want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; jan1&amp;nbsp;&amp;nbsp; jan2&amp;nbsp;&amp;nbsp;&amp;nbsp; feb1&amp;nbsp;&amp;nbsp; feb2&amp;nbsp;&amp;nbsp;&amp;nbsp; mar1&amp;nbsp;&amp;nbsp;&amp;nbsp; mar2&amp;nbsp;&amp;nbsp;&amp;nbsp; apr1&amp;nbsp;&amp;nbsp;&amp;nbsp; apr1&lt;/P&gt;&lt;P&gt;1960&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so just the values that are bigger then the previous ant next value are left...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2016 15:29:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269482#M53396</guid>
      <dc:creator>Sandra_L</dc:creator>
      <dc:date>2016-05-10T15:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269483#M53397</link>
      <description>&lt;P&gt;totaly right! thats the next competition, so far i have no idea &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2016 15:31:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269483#M53397</guid>
      <dc:creator>Sandra_L</dc:creator>
      <dc:date>2016-05-10T15:31:45Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269486#M53399</link>
      <description>i have the precipitaion data for a lot of station and i want to generate areas with the same seasonal prec patterns. so i trie to find out the time when the maximum rain in spring and autumn falls.&lt;BR /&gt;see at my answere above what i imagine as result... the Lag Function i first have to explore...</description>
      <pubDate>Tue, 10 May 2016 15:39:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269486#M53399</guid>
      <dc:creator>Sandra_L</dc:creator>
      <dc:date>2016-05-10T15:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269491#M53402</link>
      <description>&lt;P&gt;Here's one approach. You can always decide later what to do with the original numeric values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array local {24} $ 1;&lt;/P&gt;
&lt;P&gt;do _n_=1 to 24;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; local{_n_} = 'N';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;array maxi {24} /* same variables as before */;&lt;/P&gt;
&lt;P&gt;do _n_=2 to 23;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if (maxi{_n_-1} &amp;lt; maxi{_n_}) and (maxi{_n_} &amp;gt; maxi{_n_+1}) then local{_n_}='Y';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, you still have to contend with the endpoints (JAN1 and DEC2) as well as plateaus.&amp;nbsp; For example, what should happen if the pattern is 15, 18, 18, 15?&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2016 15:52:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269491#M53402</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-05-10T15:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269493#M53403</link>
      <description>well jan1, i need to compare to dec2 of the previous year, but each year i a different observation...</description>
      <pubDate>Tue, 10 May 2016 15:53:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269493#M53403</guid>
      <dc:creator>Sandra_L</dc:creator>
      <dc:date>2016-05-10T15:53:23Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269705#M53467</link>
      <description>&lt;P&gt;Hi!&lt;BR /&gt;This works so far. i added something to take into account the plateaus but i dont get how to tell SAS to take the original values instead of the 'Y'.&lt;BR /&gt;For takin into account the jan1&amp;nbsp; (dec2) problem i shoult add tomething like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if maxi [_n_] &amp;gt; (maxi [_n_-1] &lt;FONT color="#FF9900"&gt;where jahr=jahr-&lt;/FONT&gt;&lt;FONT color="#000000"&gt;&lt;FONT color="#FF9900"&gt;1&lt;/FONT&gt;) then local [_n_] = 1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;looks like this now:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test3;&lt;BR /&gt;&amp;nbsp;set test2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; array local [24];&lt;BR /&gt;&amp;nbsp;&amp;nbsp; do _n_=1 to 24;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; local[_n_] = 0 ;&lt;BR /&gt;end;&lt;BR /&gt;&amp;nbsp; array maxi [24] jan1 jan2 feb1 feb2 mar1 mar2 apr1 apr2 may1 may2 jun1 jun2&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; jul1 jul2 aug1 aug2 sep1 sep2 oct1 oct2 nov1 nov2 dec1 dec2;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; do _n_=2 to 23;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if (maxi [_n_-1] &amp;lt; maxi [_n_]) and (maxi [_n_]&amp;gt; maxi [_n_+1]) then local [_n_] = 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if maxi [_n_] = maxi [_n_-1] then local [_n_] = 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if maxi [_n_] = maxi [_n_+1] then local [_n_] = 1;&lt;BR /&gt;&amp;nbsp;end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; do _n_=1;&lt;FONT color="#339966"&gt; /* same for 24 */&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if maxi [_n_] &amp;gt; maxi [_n_-1]&amp;nbsp;&lt;FONT color="#339966"&gt; /* here take the value from a line above or year (jahr) bevore*/ &lt;/FONT&gt;then local [_n_] = 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if maxi [_n_] &amp;gt; maxi [_n_+1] then local [_n_] = 1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if maxi [_n_] = maxi [_n_+1] then local [_n_] = 1;&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2016 12:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269705#M53467</guid>
      <dc:creator>Sandra_L</dc:creator>
      <dc:date>2016-05-11T12:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269708#M53468</link>
      <description>&lt;P&gt;Here's a strategy to take the original values instead of "Y".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Keep the LOCAL array numeric (as you have now done), but don't set the values to 0.&amp;nbsp; Leave them missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of setting LOCAL array elements to 1, set them to the same element of the MAXI array.&amp;nbsp; When you're done, the LOCAL array will contain all the local maxima, with missing values for the non-maxima.&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2016 13:00:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269708#M53468</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-05-11T13:00:54Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269728#M53473</link>
      <description>easy, thanks! for the jan1 problem i got the tip to try it with RETAIN SATEMENT... don't know jet, but i will find out &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;chears,&lt;BR /&gt;Sandra&lt;BR /&gt;</description>
      <pubDate>Wed, 11 May 2016 14:01:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269728#M53473</guid>
      <dc:creator>Sandra_L</dc:creator>
      <dc:date>2016-05-11T14:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269736#M53476</link>
      <description>&lt;P&gt;For the JAN1 problem, I think you will need this statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;prior_dec2 = lag(dec2);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;RETAIN will run into problems, since each observation replaces the previous DEC2 value with a new value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2016 14:12:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269736#M53476</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-05-11T14:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: find maximum in time series with arrey loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269772#M53488</link>
      <description>works perfect with lag for the previous obs! yeah!</description>
      <pubDate>Wed, 11 May 2016 15:56:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-maximum-in-time-series-with-arrey-loop/m-p/269772#M53488</guid>
      <dc:creator>Sandra_L</dc:creator>
      <dc:date>2016-05-11T15:56:11Z</dc:date>
    </item>
  </channel>
</rss>

