Lesson 19: Processing Variables with Arrays

Overview Section

In this lesson, we'll learn about basic array processing in SAS. In DATA step programming, you often need to perform the same action on more than one variable at a time. Although you can process the variables individually, it is typically easier to handle the variables as a group. Arrays offer you that option. For example, until now, if you wanted to take the square root of the 50 numeric variables in your SAS data set, you'd have to write 50 SAS assignment statements to accomplish the task. Instead, you can use an array to simplify your task.

Arrays can be used to simplify your code when you need to:

  • perform repetitive calculations
  • create many variables that have the same attributes
  • read data
  • transpose "fat" data sets to "tall" data sets, that is, change the variables in a data set to observations
  • transpose "tall" data sets to "fat" data sets, that is, change the observations in a data set to variables
  • compare variables

In this lesson, we'll learn how to accomplish such tasks using arrays. Using arrays in appropriate situations can seriously simplify and shorten your SAS programs!

Objectives

Upon completion of this lesson, you should be able to:

Learning Objectives & Outcomes

Upon completing this lesson, you should be able to do the following:

  • use an ARRAY statement to define a one-dimensional array
  • use an iterative DO loop to process through a one-dimensional array
  • determine the dimension of a one-dimensional array
  • recall that an array exists only for the duration of the DATA step
  • use a numbered range list as shorthand for a list of variables ending in consecutive numbers
  • use a named range list as shorthand for a list of variables that appear in consecutive order in your data set
  • use the special name lists _ALL_, _NUMERIC_, and _CHARACTER_ as shorthand for a list of variables
  • identify the inner workings of the compile and execution phases of a DATA step that involves an array
  • write an ARRAY statement so that SAS creates new variables rather than use already existing variables
  • use the _TEMPORARY_ array option to tell SAS to create an array with only temporary elements
  • identify the advantages and limitations of using temporary array elements
  • initialize a one-dimensional array
  • use array and BY-group processing to transpose a tall data set into a fat data set, and vice versa
  • use an ARRAY statement to define a two-dimensional array
  • understand how SAS assigns the elements to a two-dimensional array
  • use and reference a two-dimensional array
  • use the DIM function to determine the number of elements in a one-dimensional array dynamically
  • modify the lower and upper bounds of an array dimension
  • use the LBOUND and HBOUND functions to determine the lower and upper bounds of an array dimension dynamically
  • use an * in an ARRAY statement to tell SAS to determine the dimension of a one-dimensional array dynamically