SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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(:)) and the code for mean is mean(mean(g).

Thank you for any assistance. Take care.

Todd
3 REPLIES 3
Olivier
Pyrite | Level 9
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).
Rick_SAS
SAS Super FREQ
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-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 9232 views
  • 0 likes
  • 3 in conversation