rockfido 发表于 2010-5-28 04:51
TYPE VALUE
A 1
A 49
A 23
B 44
B 2
C 3
C 4
C 10
请教这样一个DATA SET,有没有办法只用一个DATA STEP(或者尽可能少的STEP)就增加一个VARIABLE MEAN_
DEVIATION,就是用VALUE的值,减去这个值所在的TYPE的那个组的平均值?
You need to load data twice, one for mean calculation and one for variance calculation. Here is one in data step. However, SAS/SQL provides a much simpler solution.
data have;
input TYPE $ VALUE;
datalines;
A 1
A 49
A 23
B 44
B 2
C 3
C 4
C 10
;
data wanted;
do until (end);
sum=0;
do i=1 by 1 until(last.type);
set have end=end;
by type;
sum+value;
if last.type then mean=sum/i;
end;
do i=1 by 1 until(last.type);
set have end=end;
by type;
var=(mean-value)**2;
output;
end;
end;
stop;
drop sum i;
run;
proc print;run;