Lists and Data Structures

Overview

The matrix is the fundamental data type for SAS/IML computations. A matrix must contain either all numeric or all character values. You cannot create a matrix in which one column has numeric values and another column has character values.

However, beginning with SAS/IML 14.2, you can create lists. The objects in a list can be of different sizes and types. A list can contain numeric matrices, character matrices, tables, and other lists.

A SAS/IML list is similar to a dynamic array. A dynamic array (also called a growable array or a mutable array) is a random-access array that can grow and shrink. Elements in a dynamic array are directly accessed by specifying their position (index). Because matrices are sometimes called arrays, the word "list" is used to describe the SAS/IML data structure that can contain other data structures.

Lists are a convenient way to store related data and to pass that data to modules, but they do not support arithmetic operations. For example, you cannot add or multiply two lists.

SAS/IML provides a rich set of functions for creating and working with lists. You can also use syntax for some list operations. For example, you can use square brackets to create a list (L = ["Ron", 3, {100 94}];), or you can use the ListCreate and ListAddItem functions to construct the same list. You can use the list item operator ($) to modify an existing item (L$3 = {100 95}) or you can use the ListSetItem subroutine.

You can use the ListAddItem and ListInsertItem subroutines to insert new items into a list. You can use the ListDeleteItem subroutine to remove items from a list.

You can use the list structure and the associated SAS/IML functions to emulate many different data structures, including the following:

  • Associative arrays. An associative array (also called a map or a dictionary) is a set of key-value pairs. Elements in an associative array are accessed by specifying the key. In the SAS/IML language, you can use the ListSetName subroutine to assign names to some or all elements. You can then access the elements by name.

  • Structs. A struct is a collection of named elements called members. The members can be inhomogeneous, which means they do not have to be the same type or size. For example, you could create a list that contains named fields for a person’s name, address, telephone number, and salary. You can use the ListSetName subroutine to assign names to items.

  • Stacks. A stack is a linear array in which objects can be inserted and removed only at the beginning of the array. A push operation adds an item to the front of the list; a pop operation removes the item at the front of the list. A stack obeys the last-in first-out principle (LIFO). You can access only the first element of a stack. In the SAS/IML language, you can use the ListGetItem function to implement the pop operation and use the ListInsertItem operation to implement the push operation.

  • Queues. A queue is a linear array in which objects can be inserted at the end of the array and removed from the beginning of the array. A queue obeys the first-in first-out principle (FIFO) but is otherwise similar to a stack.

  • Trees. A tree contains nodes and directed edges. A tree starts with a root node. The root node is connected via branches to other nodes, called child nodes. Every node except the root node has exactly one parent node. In SAS/IML, lists can contains sublists. For an example, see the section Construct a Binary Search Tree.

Last updated: July 20, 2026