chrisricard.com
Archive aisle lined with shelves of case boxes and card-catalog drawers

Getting Started With the SAS/IML Matrix Programming Language

July 27, 2026 · 417 KB

In SAS/IML, every variable is a matrix. This free 2010 chapter from Rick Wicklin's SAS Press book teaches the basics of the language, from creating matrices to solving equations efficiently.

Overview

In the SAS/IML language, every variable is a matrix. A single number is a 1 × 1 matrix, and a matrix can change size or type the moment a new value is assigned. That idea runs through this chapter, the second in Rick Wicklin's 2010 book Statistical Programming with SAS/IML Software, published by SAS Institute. SAS Press released it as a free sample chapter, and every page carries a copyright notice and a pointer to buy the book.

What SAS/IML Is

SAS/IML is a language for matrix and vector calculations. In statistical work, each row of a matrix is often an observation and each column a variable. Many methods, such as ordinary least squares regression and principal component analysis, can be written as matrix operations. The chapter covers the syntax a newcomer needs.

Creating Matrices

It starts with creating matrices. Commas mark a new row, and matrices may hold numbers or character strings. The PRINT statement has options for column and row headings, formats and labels. The NROW and NCOL functions report a matrix's dimensions. An empty matrix, one never defined, freed from memory or produced by an empty query, has zero rows and zero columns.

Functions build matrices for you. J creates a constant matrix. The DO function and the colon operator create sequences, such as 1:5 or the odd numbers from 1 to 9. The RANDGEN subroutine is recommended when generating millions of pseudorandom numbers. Other sections show how to transpose a matrix and how SHAPE turns, for example, a 1 × 12 row into a 4 × 3 matrix.

Extracting Data

Extracting data gets careful treatment. Vectors of indices pull out chosen rows and columns, and leaving out an index selects all rows or all columns. VECDIAG extracts a diagonal, while DIAG builds a diagonal matrix. The author walks through a full error message from referring to an element of an undefined matrix.

Operators

Comparison operators apply element by element. In an IF-THEN/ELSE statement, a matrix test is true only if every element is true. The ANY function checks whether at least one element is nonzero. The chapter also covers the iterative DO statement and logical operators.

Two operators join matrices. The || operator adds columns, and // adds rows. Set functions UNION, XSECT and SETDIF return unions, intersections and differences. Because an intersection can be empty, the author advises checking the result before printing or using it.

On arithmetic, the chapter separates elementwise operators, such as # for multiplication and ## for powers, from true matrix multiplication with *. Its worked example of regression shows two ways to solve the normal equations. Computing an inverse with INV works, but the SOLVE function is more efficient.

The Workspace

The last section covers managing the workspace. FREE releases memory, SHOW NAMES lists matrices, and STORE and LOAD save and restore matrices in a permanent SAS catalog between sessions.

Programming tips appear throughout, with code samples and output figures for each step.

Why It Matters

It gives SAS users a step-by-step entry into matrix programming for custom statistics that no built-in procedure provides.

Page 1 of Getting Started With the SAS/IML Matrix Programming Language
Page 1

The full text is not hosted here for copyright reasons.

More from Computer Science & Engineering Research

See all 37 in Computer Science & Engineering Research →