Chapter Contents |
Previous |
Next |
General Statistics Examples |
This example defines modules to compute correlation coefficients between numeric variables and standardized values for a set of data.
/* Module to compute correlations */ start corr; n=nrow(x); /* number of observations */ sum=x[+,] ; /* compute column sums */ xpx=t(x)*x-t(sum)*sum/n; /* compute sscp matrix */ s=diag(1/sqrt(vecdiag(xpx))); /* scaling matrix */ corr=s*xpx*s; /* correlation matrix */ print "Correlation Matrix",,corr[rowname=nm colname=nm] ; finish corr; x /* Module to standardize data */ start std; mean=x[+,] /n; /* means for columns */ x=x-repeat(mean,n,1); /* center x to mean zero */ ss=x[##,] ; /* sum of squares for columns */ std=sqrt(ss/(n-1)); /* standard deviation estimate*/ x=x*diag(1/std); /* scaling to std dev 1 */ print ,"Standardized Data",,X[colname=nm] ; finish std; /* Sample run */ x = { 1 2 3, 3 2 1, 4 2 1, 0 4 1, 24 1 0, 1 3 8}; nm={age weight height}; run corr; run std;The results are shown below.
Chapter Contents |
Previous |
Next |
Top |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.