Help using Base SAS procedures

Newbie question: How use the STD function for matrices?

Reply
N/A
Posts: 0

Newbie question: How use the STD function for matrices?

I am very new with SAS. Something I don't grasp is the following:

I am using IML. I have a matrix like this:
X = {40 1.4 1003,
45 0.8, 1123,
51 1.2 1175,
52 1.1 1014);

I want to compute the standard deviation for every column. There is the STD function. But how can it be used in such a case?

I tried something like this:
STD_X = j(1, ncol(X), -1);
do i=1 to ncol(X);
STD_X[1,i] = std(X[*,i]);
end;

However, this does not work. It seems that the type X[*,i] is no valid argument
for function STD (neither is t(X[*,i])). If I am not mistaken, X[*,i] returns a (sub-) matrix of values whereas STD expects values or variables containing values.

Can't I thus use the STD function?
Super Contributor
Posts: 281

Re: Newbie question: How use the STD function for matrices?

I don't think the STD function works in IML the way you want it to work.

I think you have to code up the standard deviation formula in IML. I know there is an example somewhere in the IML documentation where the std dev formula is presented in IML code.
N/A
Posts: 0

Re: Newbie question: How use the STD function for matrices?

Ok, thanks for your answer. I just found something in the documentation:

//-----------------------------------------------------------------
Also, SAS/IML software does not support the OF clause of the SAS DATA step. For example, the statement

a=mean(of x1-x10); /* invalid in IML */

cannot be interpreted properly in IML. The term (X1-X10) would be interpreted as subtraction of the two matrix arguments rather than its DATA step meaning, "X1 through X10."
//-----------------------------------------------------------------


It's the same thing with the mean function as with the std function. As it seems, there is no such idea as computing all elements of a matrix row or matrix column at once with those functions. (It however works with others like sum.)
SAS Super FREQ
Posts: 3,475

Re: Newbie question: How use the STD function for matrices?

Since SAS/IML 9.22, IML has supported a VAR function, which computes the variance of each column of a matrix. Use SQRT(VAR(X)) to get the standard deviation.

For how to compute the variance of each column prior to SAS/IML 9.22, see my blog post on "Computing the Variance of Each Column of a Matrix".

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