Performing the Same Action for a Series of
Variables
Using a Series of IF-THEN Statements
In the data set MYLIB.ATTRACTIONS, the variables Museums, Galleries, and Other
contain missing values when the tour does not feature that type of attraction. To change
the missing values to 0, you can write a series of IF-THEN statements with assignment
statements, as the following program illustrates:
/* same action for different variables */
data changes;
set mylib.attractions;
if Museums = . then Museums = 0;
if Galleries = . then Galleries = 0;
if Other = . then Other = 0;
run;
The pattern of action is the same in the three IF-THEN statements. Only the variable
name is different. To make the program easier to read, you can write SAS statements that
perform the same action several times, changing only the variable that is affected. This
technique is called array processing, and consists of the following three steps:
1. grouping variables into arrays
2. repeating the action
3. selecting the current variable to be acted upon
Grouping Variables into Arrays
In DATA step programming, you can put variables into a temporary group called an
array. To define an array, use an ARRAY statement. A simple ARRAY statement has the
following form:
ARRAY array-name{number-of-variables} variable-1 < . . . variable-n>;
Array-name is a SAS name that you choose to identify the group of variables. Number-
of-variables, enclosed in braces, tells SAS how many variables you are grouping, and
variable-1< . . . variable-n> lists their names.
Note: If you have worked with arrays in other programming languages, note that arrays
in SAS are different from those in many other languages. In SAS, an array is simply
a convenient way of temporarily identifying a group of variables by assigning an
alias to them. It is not a permanent data structure. It exists only for the duration of the
DATA step. The array-name identifies the array and distinguishes it from any other
arrays in the same DATA step. It is not a variable.
The following ARRAY statement lists the three variables Museums, Galleries, and
Other:
array changelist{3} Museums Galleries Other;
This statement tells SAS to do the following:
make a group named CHANGLIST for the duration of this DATA step
Performing the Same Action for a Series of Variables 219

Get Step-by-Step Programming with Base SAS 9.4, Second Edition, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.