<?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: Calculate average of variable and add as observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238553#M43833</link>
    <description>Why are you doing this? Do you actually need a dataset or do you need a printed report? If it's a printed report then you need a proc report or possibly a print instead of this. Regardless you don't need a macro but you do need to clarify your requirements.</description>
    <pubDate>Wed, 09 Dec 2015 18:22:50 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2015-12-09T18:22:50Z</dc:date>
    <item>
      <title>Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238532#M43826</link>
      <description>&lt;P&gt;Dear Alll,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to calculate mean of variable and add the same as last observation as below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Input dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Subject Score&amp;nbsp;&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Subject Score&amp;nbsp;&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;Average of A="MEAN"&amp;nbsp;&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Average of b="MEAN"&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Dec 2015 16:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238532#M43826</guid>
      <dc:creator>draroda</dc:creator>
      <dc:date>2015-12-09T16:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238535#M43827</link>
      <description>&lt;P&gt;Here's a way to get the output you are looking for in a data set.&amp;nbsp; If you want a report, I would look into different procedures.&amp;nbsp; Hope this helps!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject$ Score;
datalines;
a 1
a .
a 3
a 7
b 4
b .
b 1
b 7
;
run;

proc sql noprint;
select mean(Score) format 6.2 into: a_mean
from have
where Subject="a";

select mean(Score) format 6.2 into: b_mean
from have
where Subject="b";
quit;

data a_mean;
Subject="Average of A=";
Score=&amp;amp;a_mean;
run;

data b_mean;
Subject="Averge of B=";
Score=&amp;amp;b_mean;
run;

data want;
length Subject $25;
set have (where=(Subject="a"))
	a_mean
	have (where=(Subject="b"))
	b_mean;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Dec 2015 17:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238535#M43827</guid>
      <dc:creator>dcruik</dc:creator>
      <dc:date>2015-12-09T17:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238536#M43828</link>
      <description>&lt;P&gt;Here is a solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject$ Score;
cards;
a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1
a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .
a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3
a&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7
b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4
b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .
b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1
b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7
;

proc sql;
create table prep as
select *,avg(coalesce(score,0)) as mean format = number.2
from have
group by subject;

data want;
length subject $50.;
set prep;
by subject;
if last.subject then do;output;
&amp;nbsp;&amp;nbsp; &amp;nbsp;subject = catx(' ','Average of',subject,'= ');
&amp;nbsp;&amp;nbsp; &amp;nbsp;score= mean;
end;
drop mean;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Be sure to note the cautions/advice for trying to keep up with current Mean values for these variables.&amp;nbsp; PROC REPORT is suited to create mean (and other aggregations) summaries for reporting purposes.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Sep 2018 15:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238536#M43828</guid>
      <dc:creator>Steelers_In_DC</dc:creator>
      <dc:date>2018-09-28T15:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238548#M43831</link>
      <description>&lt;P&gt;Great.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But i may have more subjects when data gets entered , so is it possible to create macro for same ?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Dec 2015 18:09:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238548#M43831</guid>
      <dc:creator>draroda</dc:creator>
      <dc:date>2015-12-09T18:09:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238549#M43832</link>
      <description>&lt;P&gt;Perfect .&lt;/P&gt;</description>
      <pubDate>Wed, 09 Dec 2015 18:10:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238549#M43832</guid>
      <dc:creator>draroda</dc:creator>
      <dc:date>2015-12-09T18:10:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238553#M43833</link>
      <description>Why are you doing this? Do you actually need a dataset or do you need a printed report? If it's a printed report then you need a proc report or possibly a print instead of this. Regardless you don't need a macro but you do need to clarify your requirements.</description>
      <pubDate>Wed, 09 Dec 2015 18:22:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238553#M43833</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-09T18:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238556#M43834</link>
      <description>&lt;P&gt;This will loop through each individual subject and create the same output as many times as there are unique subjects.&amp;nbsp; As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt; mentioned though, there could be a better way to do this if you don't need it in a data set that doesn't use macros and such.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subject$ Score;
datalines;
a 1
a .
a 3
a 7
b 4
b .
b 1
b 7
c 3
c 7
c .
c 1
;
run;

options mprint symbolgen mlogic;
%macro subject;
proc sql;
create table subjects as
select distinct Subject
from have;
quit;

data _NULL_;
set subjects end=last;
call symputx(cats("Subject",_N_),Subject);
If last then call symputx("n",_N_);
run;

%do i=1 %to &amp;amp;n;
	proc sql noprint;
	select mean(Score) format 6.2 into: mean
	from have
	where Subject="&amp;amp;&amp;amp;Subject&amp;amp;i";
	quit;

	data mean;
	Subject="Average of &amp;amp;&amp;amp;Subject&amp;amp;i=";
	Score=&amp;amp;mean;
	run;

	%if &amp;amp;i=1 %then %do;
		data want;
		length Subject $25;
		set have (where=(Subject="&amp;amp;&amp;amp;Subject&amp;amp;i"))
			mean;
		run;
	%end;
	%else %do;
		data combine;
		length Subject $25;
		set have (where=(Subject="&amp;amp;&amp;amp;Subject&amp;amp;i"))
			mean;
		run;

		data want;
		set want
			combine;
		run;
	%end;
%end;
%mend;
%subject;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Dec 2015 18:41:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238556#M43834</guid>
      <dc:creator>dcruik</dc:creator>
      <dc:date>2015-12-09T18:41:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238562#M43837</link>
      <description>&lt;P&gt;No, it's not.&lt;/P&gt;
&lt;P&gt;Doing aggregations for table display is a job for a reporting procedure, like PROC REPORT, and should not be stored in a table.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Dec 2015 19:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238562#M43837</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-12-09T19:01:41Z</dc:date>
    </item>
    <item>
      <title>Betreff: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238683#M43848</link>
      <description>&lt;P&gt;Having those mean-rows in the dataset does not sound useful for further usage of that dataset. Anything, except for printing, will require additional code to skip the mean-rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Solution using DOW-loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
   count = 0;
   scoreSum = 0;
   do _n_ = 1 by 1 until(last.Subject);
      set work.have;
      by Subject;

      count = count +1;
      scoreSum = sum(scoreSum, Score);

      output;
   end; 

   Subject = catx(" ", "Average of ", Subject);
   Score = scoreSum / count;
   output;

   drop count scoreSum;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Dec 2015 13:32:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/238683#M43848</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2015-12-10T13:32:07Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate average of variable and add as observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/477997#M123202</link>
      <description>Thank you very much, extremely helpful!!</description>
      <pubDate>Fri, 13 Jul 2018 18:48:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-average-of-variable-and-add-as-observation/m-p/477997#M123202</guid>
      <dc:creator>bradkes</dc:creator>
      <dc:date>2018-07-13T18:48:11Z</dc:date>
    </item>
  </channel>
</rss>

