<?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 mean according to condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241215#M44684</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is producing what you want. But I completely agree with LinusH, ... you should create a report instead of 'enriching' your dataset with an extra line below each subject containing his/her/its average.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I don't recommend using the last data step. You don't want to spoil a dataset by adding extra rows containing an average while the other rows contain raw scores.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ab;
infile datalines truncover;
input Subj $ score;
datalines;
a      1
a       
a      3
a      4
b      
b      4
b      
b      5
;
run;

proc means data=work.ab nway noprint;
 class Subj;
 var score;
 output out=work.absummary mean= n= nmiss= / autoname;
run;

PROC SQL noprint;
 create table work.abmerge as
 select a.* , b.*
 from   work.ab         a
      , work.absummary  b
 where a.Subj = b.Subj
 order by a.Subj , a.Score;
quit;

data work.abmerge;
 length Subj $ 25;
 set work.abmerge;
 by Subj;
 if last.Subj then do; 
  output; 
  if score_N &amp;gt;=3 then score=Score_Mean; 
  else score=.; 
  Subj="Avg. of above Subject"; 
  output;
 end;
if index(Subj,'Avg')=0 then output; 
run;
/* end of program */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
    <pubDate>Wed, 30 Dec 2015 12:21:39 GMT</pubDate>
    <dc:creator>sbxkoenk</dc:creator>
    <dc:date>2015-12-30T12:21:39Z</dc:date>
    <item>
      <title>calculate mean according to condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241212#M44682</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have one dataset as below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Subj score&lt;/P&gt;&lt;P&gt;a &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;3&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my requirement is that i want to calculate average of score only for subject where number of non missing values equal to 3 or greatee than&amp;nbsp;.If number of non missing value is less&amp;nbsp;&amp;nbsp;than 3 then &amp;nbsp; will set the mean as . and add the same as observation after last record for particular subj.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is output&lt;/P&gt;&lt;P&gt;a &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;3&lt;/P&gt;&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;average of a &amp;nbsp;"value"&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;average of b .&lt;/P&gt;</description>
      <pubDate>Wed, 30 Dec 2015 11:31:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241212#M44682</guid>
      <dc:creator>draroda</dc:creator>
      <dc:date>2015-12-30T11:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: calculate mean according to condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241213#M44683</link>
      <description>&lt;P&gt;The&amp;nbsp;calculation can be done in SQL. But you also show an&amp;nbsp;example of a report layout, which is&amp;nbsp;probably better to do separately, like in PROC REPORT (and there are others that can you guide better than me).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For SQL, use agregate filtering with HAVING&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e.&lt;/P&gt;
&lt;P&gt;group&amp;nbsp;by id&lt;/P&gt;
&lt;P&gt;having sum(case when missing(score) then 0 else 1 end) ge 3&lt;/P&gt;</description>
      <pubDate>Wed, 30 Dec 2015 11:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241213#M44683</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-12-30T11:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: calculate mean according to condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241215#M44684</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is producing what you want. But I completely agree with LinusH, ... you should create a report instead of 'enriching' your dataset with an extra line below each subject containing his/her/its average.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I don't recommend using the last data step. You don't want to spoil a dataset by adding extra rows containing an average while the other rows contain raw scores.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ab;
infile datalines truncover;
input Subj $ score;
datalines;
a      1
a       
a      3
a      4
b      
b      4
b      
b      5
;
run;

proc means data=work.ab nway noprint;
 class Subj;
 var score;
 output out=work.absummary mean= n= nmiss= / autoname;
run;

PROC SQL noprint;
 create table work.abmerge as
 select a.* , b.*
 from   work.ab         a
      , work.absummary  b
 where a.Subj = b.Subj
 order by a.Subj , a.Score;
quit;

data work.abmerge;
 length Subj $ 25;
 set work.abmerge;
 by Subj;
 if last.Subj then do; 
  output; 
  if score_N &amp;gt;=3 then score=Score_Mean; 
  else score=.; 
  Subj="Avg. of above Subject"; 
  output;
 end;
if index(Subj,'Avg')=0 then output; 
run;
/* end of program */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Wed, 30 Dec 2015 12:21:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241215#M44684</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2015-12-30T12:21:39Z</dc:date>
    </item>
    <item>
      <title>Re: calculate mean according to condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241222#M44688</link>
      <description>&lt;P&gt;Echo with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH﻿&lt;/a&gt;&amp;nbsp;and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk﻿&lt;/a&gt;, this&amp;nbsp;should be a report request.&amp;nbsp;Here is a one-pass data step alternative:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ab;
	infile datalines truncover;
	length Subj $ 50;
	input Subj score;
	datalines;
a      1
a       
a      3
a      4
b      
b      4
b      
b      5
;
run;

data want;
	do until (last.subj);
		set ab;
		by subj notsorted;

		if score&amp;gt;=3 then
			do;
				_ct+1;
				_tot+score;
			end;

		output;
	end;

	subj='Avg. of above Subject';
	score=_tot/_ct;
	output;
	call missing(of _:);
	drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Dec 2015 15:06:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241222#M44688</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2015-12-30T15:06:58Z</dc:date>
    </item>
    <item>
      <title>Re: calculate mean according to condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241332#M44710</link>
      <description>&lt;P&gt;Want make a REPORT ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Subj $ score;
cards;
a      1
a       .
a      3
a      4
b      .
b      4
b      .
b      5
;
run;
proc report data=have nowd ;
column Subj score ;
define Subj/order;
define score/analysis mean;
compute score;
 if not missing(score.mean) then n+1; 
 if not missing(_BREAK_) then do;
  if n lt 4 then call missing(score.mean);
  n=0;
 end;
endcomp;
break after Subj/summarize;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Dec 2015 02:55:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-mean-according-to-condition/m-p/241332#M44710</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2015-12-31T02:55:12Z</dc:date>
    </item>
  </channel>
</rss>

