BookmarkSubscribeRSS Feed
bigbigben
Obsidian | Level 7

Hi, folks:

     I try to use some simple function to compute statistics for a random variable, i.e, var for variance, std for standard deviation or mean for average. I do find some instructions online, like the following links:

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

or

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

How ever, when I type the following lines in the IML mode, error messages pop out:

yy=std(xx);

ERROR: Not enough arguments for function STD.

where xx is a 100*1 vector generated by xx=normal(j(100,1,10));

The same thing happens if I use the var function.

However, if I instead type yy=std(xx,1) or zz=var(xx,1). Here,  I copied the covention from MATLAB where the second argument indicates we should do compuation based on rows. SAS does give me some results this time, but these results are very weird: both yy and zz are 100*1 vectors rather than scalars I expected. I use SAS under unix and the version should be SAS 9.1 or 9.2. Is there any documentation with more details to explain how to use these functions or someone can tell me the correct way to use these functions?

Thanks.

2 REPLIES 2
TomTom
Calcite | Level 5

The problem is that the STD function has only been added to IML in SAS v9.3, you are using an earlier version. Therefore you are calling the SAS/base function STD which has different arguments. Here is its documentation

http://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p1fani7wj5h...

Same problem with the VAR function although this function has been added at some point earlier. I am using SAS v9.23 and for me VAR is available in IML and STD isn't.

Rick_SAS
SAS Super FREQ

For earlier releases of SAS/IML, you can use the following module, which appears in my book Statistical Programming with SAS/IML Software:

/* Var: return sample variance of each column of a data matrix */

/*   x     is a matrix that contains the data

*/

start Var(x);

mean = x[:,];

countn = j(1, ncol(x));      /* allocate vector for counts */

do i = 1 to ncol(x);         /* count nonmissing values    */

countn = sum(x[,i]^=.);/* in each column             */

end;

var = (x-mean)[##,] / (countn-1);

return ( var );

finish;

For STD, use sqrt(var(x)).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1948 views
  • 3 likes
  • 3 in conversation