<?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 Average of a partial array with no common prefix in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866946#M42641</link>
    <description>&lt;P&gt;The first "M" array gets the average of all numeric variables in the dataset. There's a total of 74 variables: X,Y,Z,V,P,L...etc&lt;/P&gt;&lt;P&gt;However, I would like to get the average of only that last 6 variables, i.e. from 69 - 74th variable. I tried creating another array "L" where I identified the do statement to start from (i=69 to i+5). The average function does not work still. Any help?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data XXX;&lt;BR /&gt;Set YYYY;&lt;BR /&gt;array &lt;STRONG&gt;M(*)&lt;/STRONG&gt; _numeric_;&lt;BR /&gt;do i=1 to dim(M);&lt;BR /&gt;TOTAVG=mean(of M(*));&lt;BR /&gt;drop i;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;array &lt;STRONG&gt;L(*)&lt;/STRONG&gt; _numeric_;&lt;BR /&gt;i=69;&lt;BR /&gt;do n=i to i+5;&lt;BR /&gt;AVG2=mean(of L[n]);&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Wed, 29 Mar 2023 11:40:24 GMT</pubDate>
    <dc:creator>DDawaba</dc:creator>
    <dc:date>2023-03-29T11:40:24Z</dc:date>
    <item>
      <title>Average of a partial array with no common prefix</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866946#M42641</link>
      <description>&lt;P&gt;The first "M" array gets the average of all numeric variables in the dataset. There's a total of 74 variables: X,Y,Z,V,P,L...etc&lt;/P&gt;&lt;P&gt;However, I would like to get the average of only that last 6 variables, i.e. from 69 - 74th variable. I tried creating another array "L" where I identified the do statement to start from (i=69 to i+5). The average function does not work still. Any help?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data XXX;&lt;BR /&gt;Set YYYY;&lt;BR /&gt;array &lt;STRONG&gt;M(*)&lt;/STRONG&gt; _numeric_;&lt;BR /&gt;do i=1 to dim(M);&lt;BR /&gt;TOTAVG=mean(of M(*));&lt;BR /&gt;drop i;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;array &lt;STRONG&gt;L(*)&lt;/STRONG&gt; _numeric_;&lt;BR /&gt;i=69;&lt;BR /&gt;do n=i to i+5;&lt;BR /&gt;AVG2=mean(of L[n]);&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2023 11:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866946#M42641</guid>
      <dc:creator>DDawaba</dc:creator>
      <dc:date>2023-03-29T11:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: Average of a partial array with no common prefix</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866960#M42644</link>
      <description>&lt;P&gt;The first average does not require a DO loop - you can just use of M[*] and the list of values. This code produces the average of all numeric variables as &lt;STRONG&gt;TOTAVG&lt;/STRONG&gt;, and the average of the right-most 2 variables as &lt;STRONG&gt;Last2Avg&lt;/STRONG&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set sashelp.fish(obs=3);
	array M(*) _numeric_;
	TOTAVG=mean(of M(*));
	do _i=dim(m)-1 to dim(m);
		_sum=sum(_sum,m[_i]);
		_count=sum(_count,1);
	end;
	Last2Avg=_sum/_count;
	drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I named all the variables I didn't want to keep starting with an underscore (_) and drop them all with that last drop statment.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2023 12:41:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866960#M42644</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2023-03-29T12:41:05Z</dc:date>
    </item>
    <item>
      <title>Re: Average of a partial array with no common prefix</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866962#M42645</link>
      <description>&lt;P&gt;If you are so lucky that there are only numeric variables at the end of your dataset vector, you can use "--" to get the variables in order of definition, e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array M (*) _numeric_;
  array L(*) m201234--n345;
  Totavg=mean(of M(*));
  avg2=mean(of L(*));
run;
  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(you should not calculate the mean more than once, the code you showed would just assign the same value to the same variable over and over again)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your numeric variables are intermixed with characters, you can change the order of the variables by reading character variables in one SET statement and numeric values in another:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have(keep=_NUMERIC_);
  set have(keep=_CHARACTER_);
  array M (*) _numeric_;
  array L(*) m201234--n345;
  Totavg=mean(of M(*));
  avg2=mean(of L(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2023 12:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866962#M42645</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-03-29T12:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Average of a partial array with no common prefix</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866963#M42646</link>
      <description>&lt;P&gt;You can also make a list based on the position of the variables that only includes the numeric (or character) variables with the syntax:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array L m201234-numeric-n345;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: There is no need to type the (*) (or either of its aliases [*] or {*}) in the ARRAY statement in a data step.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2023 12:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866963#M42646</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-29T12:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: Average of a partial array with no common prefix</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866971#M42647</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441318"&gt;@DDawaba&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also use variable lists such as &lt;FONT face="courier new,courier"&gt;_numeric_&lt;/FONT&gt; directly in the MEAN function, so you don't need arrays.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set sashelp.baseball;
totavg=mean(of _numeric_);
avg2=mean(of CrBB-numeric-logSalary);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I read the variable names &lt;FONT face="courier new,courier"&gt;CrBB&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;logSalary&lt;/FONT&gt;&amp;nbsp;(the first and last item in the list of the last six numeric variables in SASHELP.BASEBALL) off PROC CONTENTS output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=sashelp.baseball varnum;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid this manual step and hardcoding of variable names you can tell PROC SQL to create an equivalent variable list for you:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint nowarn outobs=6;
select name into :last6numvars separated by ' '
from dictionary.columns
where libname='SASHELP' &amp;amp; memname='BASEBALL' &amp;amp; type='num'
order by varnum desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then replace &lt;FONT face="courier new,courier"&gt;CrBB-numeric-logSalary&lt;/FONT&gt;&amp;nbsp;by a reference to macro variable &lt;FONT face="courier new,courier"&gt;last6numvars&lt;/FONT&gt; in the assignment statement for &lt;FONT face="courier new,courier"&gt;avg2&lt;/FONT&gt;&amp;nbsp;in the DATA step above:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;avg2=mean(of &amp;amp;last6numvars);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Mar 2023 13:02:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Average-of-a-partial-array-with-no-common-prefix/m-p/866971#M42647</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-03-29T13:02:36Z</dc:date>
    </item>
  </channel>
</rss>

