Help using Base SAS procedures

PROC IML - mean and standard deviation

Reply
N/A
Posts: 0

PROC IML - mean and standard deviation

Hello, I hope all is well. I am trying to calculate a mean and standard deviation of elements in a matrix (that is the mean (and std) of all elements in a matrix), but cannot figure out if syntax exists within PROC IML. If a matrix 3x3 matrix has 9 elements (numbered 1-9) then the mean of the matrix is 5. I've tried a lot of options, but can't get it to work.

The backstory is that I need to translate MATLAB (which I don't have access to) code into IML, but again, can't find comparable functions. For example, for a matrix g, the MATLAB code for std is std(g(Smiley Happy) and the code for mean is mean(mean(g).

Thank you for any assistance. Take care.

Todd
Super Contributor
Posts: 260

Re: PROC IML - mean and standard deviation

Hi Todd.
Since IML is part of SAS, it does not expect roxs and columns to be part of the same data series. So there seems to be nothing as simple as in Matlab to compute such means and standard deviations ; it would have been done with the MEAN and STD functions if your data were arranged as {1 2 3 4 5 6 7 8 9}, in one row or one column only.
Here are workarounds to compute means & standard deviations for a whole matrix.
[pre]
PROC IML ;
a = {1 2 3 4, 5 6 7 8, 9 10 11 12} ;
mean = a[+,+]/(NROW(a)*NCOL(a)) ;
PRINT mean ;
std = SQRT((a-mean)[##,+]/(NROW(a)*NCOL(a)-1)) ;
PRINT std ;
QUIT ;
[/pre]
Regards.
Olivier

PS : the + and ## syntax is taken from the Example 8.1 in the SAS IML documentation (General Statistics > Correlation).
SAS Super FREQ
Posts: 3,477

Re: PROC IML - mean and standard deviation

You can use A[:] for the mean and M[##] for sum of squares of a matrix.
If you have missing values, then you need to be careful about the divisor for the standard deviation. Try the following:
proc iml;
a = {. 2 3 4, 5 6 . 8, 9 10 11 12} ;
mean = a[:];
numNonMissing = ncol(loc(a^=.));
std = sqrt( (a-mean)[##] / (numNonMissing-1) );
print mean std;
SAS Super FREQ
Posts: 3,477

Re: PROC IML - mean and standard deviation

Ask a Question
Discussion stats
  • 3 replies
  • 3682 views
  • 0 likes
  • 3 in conversation