The following statements show how you can define modules to standardized columns for a matrix of numeric data. For a more robust implementation, see the STANDARD function .
proc iml; /* Standardize data: Assume no column has 0 variance */ start stdMat(x); mean = mean(x); /* means for columns */ cx = x - mean; /* center x to mean zero */ std = std(x); /* standard deviation estimate*/ y = cx / std(x); /* scaling to std dev 1 */ return( y ); finish stdMat; x = { 1 2 3, 3 2 1, 4 2 1, 0 4 1, 24 1 0, 1 3 8}; nm = {age weight height}; std = stdMat(x); print std[colname=nm label="Standardized Data"];
The columns shown in Output 9.1.1 have zero mean and unit variance.
In a similar way, you can define a module that returns the correlation matrix of numeric data. The following module computes the correlation matrix according to a formula that you might see in a statistics textbook. For a more efficient implementation that supports missing values, use the built-in CORR function .
/* Compute correlations: Assume no missing values */ start corrMat(x); n = nrow(x); /* number of observations */ sum = x[+,]; /* compute column sums */ xpx = x`*x - sum`*sum/n; /* compute sscp matrix */ s = diag(1/sqrt(vecdiag(xpx))); /* scaling matrix */ corr = s*xpx*s; /* correlation matrix */ return( corr ); finish corrMat; corr = corrMat(x); print corr[rowname=nm colname=nm label="Correlation Matrix"];
There are many equivalent ways to compute a correlation matrix. If you have already written and debugged the STDMAT function, you might want to call that function during the computation of the correlation matrix. The following function is an alternative way to compute the correlation matrix of data:
/* Another way to compute correlations: Assume no missing values */ start corrMat2(x); y = StdMat(x); /* standardize columns */ corr = (y`*y)/(nrow(x)-1); /* correlation matrix */ return( corr ); finish corrMat2; c = corrMat2(x);