turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- PROC IML - mean and standard deviation

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-28-2008 05:26 PM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

03-04-2008 06:00 AM

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).

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).

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Olivier

05-29-2009 03:28 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to deleted_user

10-12-2011 09:03 AM

By the way, in SAS/IML 9.22 and beyond there are explicity MEAN and VAR functions:

MEAN: http://support.sas.com/documentation/cdl/en/imlug/64248/HTML/default/viewer.htm#imlug_langref_sect19...

VAR: http://support.sas.com/documentation/cdl/en/imlug/64248/HTML/default/viewer.htm#imlug_langref_sect32...

For computing the variance of each column prior to SAS/IML 9.22, see http://blogs.sas.com/content/iml/2011/04/07/computing-the-variance-of-each-column-of-a-matrix/