Contents
This book presents a few examples that use functions that were introduced in SAS/IML 9.22. If you have not yet upgraded to SAS/IML 9.22 or SAS 9.3, you might want to define and run the following modules, which implement portions of the functionality of the MEAN and VAR functions and the QNTL subroutine.
The function definitions will fail if you are running SAS/IML 9.22 or later because the SAS/IML language does not permit you to redefine built-in functions.
/* Mean: return sample mean of each column of a data matrix */
/* For this module
* x is a matrix that contains the data
*/
start Mean(x);
mean = x[:,];
return ( mean );
finish;
/* Var: return sample variance of each column of a data matrix */
/* For this module
* x is a matrix that contains the data
*/
start Var(x);
mean = x[:,];
countn = j(1, ncol(x)); /* allocate vector for counts */
do i = 1 to ncol(x); /* count nonmissing values */
countn[i] = sum(x[,i]^=.); /* in each column */
end;
var = (x-mean)[##,] / (countn-1);
return ( var );
finish;
/* Qntl: compute quantiles (Defn. 5 from the UNIVARIATE doc) */
/* For this module
* q upon return, q contains the specified quantiles of
* the data.
* x is a matrix. The module computes quantiles for each column.
* p specifies the quantiles. For example, 0.5 specifies the
* median, whereas {0.25 0.75} specifies the first and
* third quartiles.
* This module does not handle missing values in the data.
*/
start Qntl(q, x, p); /* definition 5 from UNIVARIATE doc */
n = nrow(x); /* assume nonmissing data */
q = j(ncol(p), ncol(x));
do j = 1 to ncol(x);
y = x[,j];
call sort(y,1);
do i = 1 to ncol(p);
k = n*p[i]; /* position in ordered data */
k1 = int(k); /* indices into ordered data */
k2 = k1 + 1;
g = k - k1;
if g>0 then
q[i,j] = y[k2]; /* return a data value */
else /* average adjacent data */
q[i,j] = (y[k1]+y[k2])/2;
end;
end;
finish;