<?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: finding maximum value in an array and averaging max-1, max and max+1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902355#M356596</link>
    <description>&lt;P&gt;Thank you - works perfectly! Sorry about the code. Here is the complete code including yours:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data test;
input var1-var10;
cards;
1 2 3 4 5 4 3 2 1 1 
1 3 4 5 6 3 5 2 2 1
;
run;
data want;
    set test;
    array v var1-var10;
    maximum_value=max(of var1-var10);
    loc=whichn(maximum_value,of var1-var10);
    avg_of_3=mean(v(loc-1),v(loc),v(loc+1));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Nov 2023 16:32:15 GMT</pubDate>
    <dc:creator>mateescu</dc:creator>
    <dc:date>2023-11-09T16:32:15Z</dc:date>
    <item>
      <title>finding maximum value in an array and averaging max-1, max and max+1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902328#M356575</link>
      <description>&lt;P&gt;I have a dataset looking like this:&lt;BR /&gt;&lt;BR /&gt;data test;&lt;/P&gt;&lt;P&gt;array var(10) var1-var10;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;1 2 3 4 5 4 3 2 1 1&lt;/P&gt;&lt;P&gt;. . . . . . . . . . .&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;BR /&gt;&lt;BR /&gt;I want to find the maximum value in the array (in this case 5) and average the 3 values around it (the max value, max-1 and max+1 - in the example 4,5 and 4)&lt;BR /&gt;&lt;BR /&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 14:35:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902328#M356575</guid>
      <dc:creator>mateescu</dc:creator>
      <dc:date>2023-11-09T14:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: finding maximum value in an array and averaging max-1, max and max+1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902329#M356576</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set test;
    array v var1-var10;
    maximum_value=max(of var1-var10);
    loc=whichn(maximum_value,of var1-var10);
    avg_of_3=mean(v(loc-1),v(loc),v(loc+1));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: this code fails if the maximum of var1-var10 is in position 1 or position 10. If the maximum appears at two or more locations, it will find only the first such occurrence of the maximum and work with that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 14:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902329#M356576</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-09T14:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: finding maximum value in an array and averaging max-1, max and max+1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902355#M356596</link>
      <description>&lt;P&gt;Thank you - works perfectly! Sorry about the code. Here is the complete code including yours:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data test;
input var1-var10;
cards;
1 2 3 4 5 4 3 2 1 1 
1 3 4 5 6 3 5 2 2 1
;
run;
data want;
    set test;
    array v var1-var10;
    maximum_value=max(of var1-var10);
    loc=whichn(maximum_value,of var1-var10);
    avg_of_3=mean(v(loc-1),v(loc),v(loc+1));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 16:32:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902355#M356596</guid>
      <dc:creator>mateescu</dc:creator>
      <dc:date>2023-11-09T16:32:15Z</dc:date>
    </item>
    <item>
      <title>Re: finding maximum value in an array and averaging max-1, max and max+1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902429#M356632</link>
      <description>&lt;P&gt;You can fix the boundary condition by adding two extra variables (with missing values) to the ARRAY.&amp;nbsp; You can use just one variable and add it twice to the array.&lt;/P&gt;
&lt;P&gt;The result be the average of the TWO values at the boundary since the third value will be missing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set test;
    array v var0 var1-var10 var0;
    maximum_value=max(of v[*]);
    loc=whichn(maximum_value,of v[*]);
    avg_of_3=mean(v(loc-1),v(loc),v(loc+1));
    drop var0  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2023 03:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902429#M356632</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-10T03:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: finding maximum value in an array and averaging max-1, max and max+1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902461#M356652</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You can fix the boundary condition by adding two extra variables (with missing values) to the ARRAY.&amp;nbsp; You can use just one variable and add it twice to the array.&lt;/P&gt;
&lt;P&gt;The result be the average of the TWO values at the boundary since the third value will be missing.&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;True if that's what the OP wanted. However, no such statement from the OP has been made, so I didn't try to code anything for boundary conditions.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2023 10:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902461#M356652</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-10T10:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: finding maximum value in an array and averaging max-1, max and max+1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902479#M356664</link>
      <description>&lt;P&gt;I did something similar (see code below) but your solution is more elegant. Thanks! I know I didn't ask for this, but just because I didn't think of that problem in the beginning. I appreciate all the help.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data want;
    set test;
    array v var1-var10;
    maximum_value=max(of var1-var10);
    loc=whichn(maximum_value,of var1-var10);
    if loc ge 2 and loc le 9 then avg_of_3=mean(v(loc-1),v(loc),v(loc+1));
	if loc=1 then avg_of_3=mean(v(loc),v(loc+1));
	if loc=10 then avg_of_3=mean(v(loc),v(loc-1));
run;	&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Nov 2023 12:20:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902479#M356664</guid>
      <dc:creator>mateescu</dc:creator>
      <dc:date>2023-11-10T12:20:23Z</dc:date>
    </item>
    <item>
      <title>Re: finding maximum value in an array and averaging max-1, max and max+1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902480#M356665</link>
      <description>&lt;P&gt;Looks good to me! Good for you to make the fix by yourself.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2023 12:21:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/finding-maximum-value-in-an-array-and-averaging-max-1-max-and/m-p/902480#M356665</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-10T12:21:40Z</dc:date>
    </item>
  </channel>
</rss>

