Biostat Macro Library
Calculate Standard Deviation and Standard Error
%Macro stdev(anary,start,stop,mmean,sstdev,sstderr);
*Macro returns the mean and standard deviation for the elements start-stop in array anary;
ssum=0;
Do ii=&start To &stop;
ssum=ssum+&anary{ii}; *find the sum;
End;
&mmean=ssum/(1.0+&stop-&start); *calculate the mean;
&sstdev=0;
Do ii=&start To &stop;
&sstdev=&sstdev+(&anary{ii}-&mmean)**2; *sum the squared differences from the mean;
End;
&sstdev=Sqrt(&sstdev/(&stop-&start)); *calculate the square root of the sum / n-1;
&sstderr=&sstdev/Sqrt(1.0+&stop-&start);
*calculate standard error;
%Mend stdev;
Data work.A; *example to call the macro;
Retain n 0 x1-x10;
Array x{10};
n=n+1;
Input x{n};
%stdev(x,1,n,mean,stdev,stderr);
Output;
Cards;
0
1
2
3
4
5
6
7
8
;
Run;
|