SAS/IML functions that operate on columns of a matrix

7

A SAS programmer asked for a list of SAS/IML functions that operate on the columns of an n x p matrix and return a 1 x p row vector of results. The functions that behave this way tend to compute univariate descriptive statistics such as the mean, median, standard deviation, and quantiles.

The following SAS/IML functions compute a statistic for each column of a matrix:

  • COUNTMISS function: counts the number of missing values (use the "col" option)
  • COUNTN function : counts the number of nonmissing values (use the "col" option)
  • COUNTUNIQUE function: counts the number of unique values (use the "col" option)
  • CV function: computes the sample coefficient of variation
  • KURTOSIS function: computes the sample kurtosis
  • MAD function: computes the univariate median absolute deviation
  • MEAN function: computes sample means
  • QNTL call: computes sample quantiles (returns multiple rows)
  • QUARTILE function: computes the five-number summary (returns five rows: min, Q1, Q2, Q3, and max))
  • SKEWNESS function: computes the sample skewness
  • STD function: computes the sample standard deviation
  • VAR function: computes the sample variance

You can also use subscript reduction operators to compute the following descriptive statistics for each column in a matrix X:

  • X[+, ] returns the column sums (similar to the SUM function)
  • X[#, ] returns the column products (similar to the PROD function)
  • X[<>, ] returns the column maxima (similar to the MAX function)
  • X[><, ] returns the column minima (similar to the MIN function)
  • X[<:>, ] returns the index of the column maxima
  • X[>:<, ] returns the index of the column minima
  • X[##, ] returns the column sum of squares (similar to the SSQ function)

Lastly, elementwise operators (+, -, #, /, ##) in SAS/IML can operate on columns.

As you might expect, many of these operations also apply to rows of a matrix if you make a minor modification. For example, to count values across each row, use the "row" option of the COUNTMISS, COUNTN, and COUNTUNIQUE functions. To use subscript reduction operators on rows, change the location of the operator. For example, X[ ,+] returns the row sums, and X[ ,##] returns the sum of squares for rows. Although the functions that compute descriptive statistics (CV, KURTOSIS, MAD, ...) do not have a row option, you can pass in the transpose of a matrix to operate on its rows.

Share

About Author

Rick Wicklin

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

7 Comments

  1. Pingback: The jackknife method to estimate standard errors in SAS - The DO Loop

  2. Hi Rick

    I am using SAS/IML. I have imported my data from excel into SAS using PROC IMPORT.

    I want then to enable SAS interactive matrix language. I would like to save my data in a matrix. The matrix will depend in the data inserted in the excel file. Sometimes I have data over 5 columns and sometimes over 6 columns. Therefore I need to read how many columns I have in the excel file. Is this possible?

    Thanks

  3. Pingback: Jackknife estimates in SAS - The DO Loop

  4. Pingback: On using the range to estimate the variability of small samples - The DO Loop

Leave A Reply

Back to Top