SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Processing Variables with Arrays
Creating One-Dimensional Arrays


Defining an Array

To group previously defined data set variables into an array, use an ARRAY statement.


General form, ARRAY statement:
ARRAY array-name{dimension} elements;

where

  • array-name specifies the name of the array
  • dimension describes the number and arrangement of array elements
  • elements lists the variables to include in the array.
Note: Do not give an array the same name as a variable in the same DATA step. Also, avoid using the name of a SAS function: the array will be correct, but you won't be able to use the function in the same DATA step and a warning message will be written to the SAS log.


For example, in the data set Finance.Sales91, you might want to process the variables Qtr1-Qtr4 in the same way.

Description of Finance.Sales91
Variable Type Length
SalesRep char 8
Qtr1 num 8
Qtr2 num 8
Qtr3 num 8
Qtr4 num 8


To group the variables in the array, first give the array a name. In this example, make the array name sales.
     array sales {4} qtr1 qtr2 qtr3 qtr4;

Following the array name, you must specify the dimension of the array. The dimension describes the number and arrangement of elements in the array. There are several ways to specify the dimension; in a one-dimensional array such as this one, you can simply specify the number of array elements. The array elements are the existing variables that you want to reference and process elsewhere in the DATA step.

     array sales {4} qtr1 qtr2 qtr3 qtr4;

You can also indicate the dimension of a one-dimensional array by using an asterisk (*), as long as you also specify the elements of the array. This way, SAS software determines the dimension of the array by counting the number of elements.

     array sales {*} qtr1 qtr2 qtr3 qtr4;

Note: Enclose the dimension in either braces, brackets, or parentheses.
                 ( )
     array sales {4} qtr1 qtr2 qtr3 qtr4;
                 [ ]

When specifying the elements of an array, you can list each variable name that you want to include in the array. When listing elements, separate each element with a space. As with all SAS statements, you end the ARRAY statement with a semicolon (;).

     array sales{4} qtr1 qtr2 qtr3 qtr4;

Array elements must be either all numeric or all character. You can also specify array elements as a variable list. Here is an example of an ARRAY statement that groups the variables Qtr1 through Qtr4 into a one-dimensional array, using a variable list.

     array sales{4} qtr1-qtr4;

  back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement