SAS Arrays: How To Create And Use An Array (With Examples)

In SAS, an array is a convenient way of temporarily identifying a group of variables. It’s not a data structure, and the array name is not a variable. An ARRAY statement defines an array. An array reference uses an array element in a program statement.

All variables that are associated with an array must be of the same type, either character or numeric. As a result, arrays are typically referred to as either character arrays or numeric arrays. Variables that are associated with an array do not have to be already existing variables.

If they do not exist within a program data vector (PDV) when the ARRAY statement is compiled, SAS creates them for you.

If I could conclude here is the short definition of SAS Array:

A SAS array is a set of variables of the same type that you want to perform the same operation on. The variables in an array are called elements and can be accessed based on their position (also known as index).

You use the name of the array to reference the set of variables. Arrays are useful to create new variables and carry out repetitive tasks

Syntax of SAS Array

To use arrays in SAS code, first make sure that you understand the basic syntax of the SAS ARRAY statement.

The ARRAY statement consists of the keyword ARRAY followed by the name of the array:

The array name is followed by either a pair of parentheses ( ), braces < >, or square brackets [ ]. In this article we use square braces < >.

/* syntax */ ARRAY ARRAY-NAME ($) VARIABLE-LIST ARRAY-VALUES ; 

Description:

Examples Of SAS Array Declaration

SAS Arrays can be declared in multiple ways depending on the data type and its values. But before that let’s discuss a few important things.

: It specifies that SAS is to determine the length by counting the variables in the array

Variables

_TEMPORARY_ : It creates a list of temporary data elements.

Here are some examples of SAS array declarations.

/* Declare an array of length 3 named “simple” with variables named red, green, and yellow */ array simple red green yellow; /* Declare an array of length 7 named “days” with variables named d1, d2, d3. d7 */ array days d1-d7; /* Declare an array named “month” with variables named jan,feb, jul, oct, nov. SAS will calculate length of an array by counting variables */ array month jan feb jul oct nov; /* Declare an array of length 5 named “rollno” with values 10, 20, 30, 40, 50 */ array rollno[5] (10 20 30 40 50); /* Declare an array of length 4 named “test” with variables named t1, t2, t3, t4 and its initial values 90,80,70,60 respectively */ array test t1-t4 (90 80 70 60); /* Declare an array of length 5 named “quests” which contain character values */ array quests(1:5) $ Q1-Q5; /* Define an array and assign initial character values to the elements in the array */ array test2 $ a1 a2 a3 ('a','b','c');