Lesson 19: Processing Variables with Arrays

Printer-friendly versionPrinter-friendly version

Introduction

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!

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
  • know 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
  • understand 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
  • understand 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

Our "to do" list for this lesson

  1. Read the lessons pages that follow.
  2. Type up your answers to the homework problems that follow the lesson in a Word file named homework19_yourPSUloginid. That is, if your PSU user id is xyz123, then name your file homework19_xyz123. Upload the file to the Lesson #19 Homework Dropbox.
  3. Post any questions or comments you have concerning the lesson's material to the Lesson #19 General Discussion Board.
  4. Take the Lesson #19 Mastery Quiz. Remember two things: i) You have 20 minutes to complete the quiz, and ii) as soon as you hit the "submit" button, your answers are submitted and graded, and the quiz becomes closed to you.