Creating One-Dimensional Arrays

ARRAY Statement Syntax

An ARRAY statement groups data set variables into an array.
Syntax, ARRAY statement:
ARRAY array-name{dimension} <elements>;
  • 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. Array elements must be either all numeric or all character. If no elements are listed, new variables are created with default names.
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 is correct, but you will not be able to use the function in the same DATA step, and a warning message appears in the SAS log.
Note: You cannot use array names in LABEL, FORMAT, DROP, KEEP, or LENGTH statements. Arrays exist only for the duration of the DATA step.

Specifying an Array Name

To group the variables in the array, first give the array a name. This example makes an array named Sales.
array sales{4} qtr1 qtr2 qtr3 qtr4;

Specifying the Dimension

Following the array name, 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, you can simply specify the number of array elements. The array elements are the variables that you want to reference and process elsewhere in the DATA step.
    array sales{4} qtr1 qtr2 qtr3 qtr4;
  • The dimension of an array does not have to be the number of array elements. You can specify a range of values for the dimension when you define the array. For example, you can define the array Sales as follows:
    array sales{96:99} totals96 totals97 totals98 totals99;
  • You can also indicate the dimension of a one-dimensional array by using an asterisk (*). In this way, SAS determines the dimension of the array by counting the number of elements.
    array sales{*} qtr1 qtr2 qtr3 qtr4;
  • Enclose the dimension in either parentheses, braces, or brackets.
               ( ) 
    array sales{4} qtr1 qtr2 qtr3 qtr4; 
               [ ]
    Note: It is a recommended best practice to use brackets instead of braces or parentheses.

Specifying Array Elements

When specifying the elements of an array, 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;
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;

Specifying Variable Lists as Array Elements

You can specify variable lists in the forms shown below. Each type of variable list is explained in more detail following the table.
Table 16.1 Variables and Their Forms
Variables
Form
a numbered range of variables
Var1-Varn
all numeric variables
_NUMERIC_
all character variables
_CHARACTER_
all character or all numeric variables
_ALL_

Specifying a Numbered Range of Variables

Qtr1 Qtr2 Qtr3 Qtr4  → Qtr1-Qtr4
  • The variables must have the same name except for the last character or characters.
  • The last character of each variable must be numeric.
  • The variables must be numbered consecutively.
array sales{4} qtr1-qtr4;
In the preceding example, you would use sales{4} to reference Qtr4. However, the index of an array does not have to range from one to the number of array elements. You can specify a range of values for the index when you define the array. For example, you can define the array sales as follows:
array sales{96:99} totals96-totals99; 

Specifying All Numeric Variables

Amount Rate Term  → _NUMERIC_
_NUMERIC_ specifies all numeric variables that have already been defined in the current DATA step.
array sales{*} _numeric_;

Specifying All Character Variables

FrstName LastName Address   →  _CHARACTER_
_CHARACTER_ specifies all character variables that have already been defined in the current DATA step.
array sales{*} _character_;

Specifying All Character or All Numeric Variables

FrstName LastName Address  →  _ALL_
Amount Rate Term  →  _ALL_
_ALL_ specifies all variables that have already been defined in the current DATA step. The variables must all be of the same type: either all character or all numeric.
array sales{*} _all_;

Examples: Referencing Elements of an Array

There are several ways to use arrays to process variables in the DATA step.
You can group variables in a one-dimensional array.
data work.report(drop=day); 
   set master.temps; 
   array wkday[7] mon tue wed thr fri sat sun; 
   do day=1 to 7; 
      if wkday{day}>95 then output; 
   end; 
run; 
You can specify array elements.
data work.weights(drop=day); 
   set master.class; 
   array wt[6] w1-w6; 
   do day=1 to 6; 
      wt{day}=wt{day}*2.2; 
   end; 
run; 
You can create numbered ranged variables.
data work.new(drop=day); 
   set master.synyms; 
   array term[9] also1-also9; 
   do day=1 to 9; 
      if term{day} ne ' ' then output; 
   end; 
run;
Arrays are powerful because you can use them to reference the elements of an array by an index value. Typically, arrays are used with DO loops to process multiple variables and to perform repetitive calculations.
array quarter[4] jan apr jul oct; 
do qtr=1 to 4; 
   YearGoal=quarter{qtr}*1.2; 
end;
When you define an array in a DATA step, a subscript value is assigned to each array element. The subscript values are assigned in the order of the array elements.
                  1   2   3   4 
array quarter[4] jan apr jul oct; 
do qtr=1 to 4; 
   YearGoal=quarter{qtr}*1.2; 
end;
You use an array reference to perform an action on an array element during execution. To reference an array element in the DATA step, specify the name of the array, followed by a subscript value enclosed in brackets.
Syntax, ARRAY reference:
array-name[subscript]
subscript
  • is enclosed in parentheses, braces, or brackets.
  • specifies a variable, a SAS expression, or an integer.
  • is within the lower and upper bounds of the dimension of the array.
When used in a DO loop, the index variable of the iterative DO statement can reference each element of the array.
array quarter{4} jan apr jul oct; 
do i=1 to 4; 
   YearGoal=quarter{i}*1.2; 
end;
For example, the DO loop above increments the index variable i from the lower bound of the quarter array, 1, to the upper bound, 4. The following sequence illustrates this process:
                  1 
array quarter{4} jan apr jul oct; 
do i=1 to 4; 
   YearGoal=quarter{1}*1.2; 
end; 

                           2 
     array quarter{4} jan apr jul oct; 
     do i=1 to 4; 
        YearGoal=quarter{2}*1.2; 
     end;

                                    3 
          array quarter{4} jan apr jul oct; 
          do i=1 to 4; 
             YearGoal=quarter{3}*1.2; 
          end; 
                                             4 
               array quarter{4} jan apr jul oct; 
               do i=1 to 4; 
                  YearGoal=quarter{4}*1.2; 
                end;
During each iteration of the DO loop, quarter{i} refers to an element of the array quarter in the order listed.
The Health Center of a company conducts a fitness class for its employees. Each week, participants are weighed so that they can monitor their progress. The weight data, currently stored in kilograms, needs to be converted to pounds.
Figure 16.1 SAS Data Set Hrd.Fitclass
SAS Data Set Hrd.Fitclass
You can use a DO loop to update the variables Weight1 through Weight6 for each observation in the Hrd.Fitclass data set.
data hrd.convert(drop=i); 
   set hrd.fitclass; 
   array wt{6} weight1-weight6; 
   do i=1 to 6; 
      wt{i}=wt{i}*2.2046; 
   end; 
run;

Compilation and Execution

To understand how the DO loop processes the array elements, examine the compilation and execution phases of this DATA step.
During compilation, the program data vector is created.
Program Data Vector
The DATA step is scanned for syntax errors.
The index values of the array elements are assigned. Note that the array name is not included in the program data vector. The array exists only for the duration of the DATA step.
During the first iteration of the DATA step, the first observation in Hrd.Fitclass is read into the program data vector.
data hrd.convert; 
   set hrd.fitclass; 
   array wt{6} weight1-weight6; 
   do i=1 to 6; 
      wt{i}=wt{i}*2.2046; 
   end; 
run;
Program Data Vector
Because the ARRAY statement is a compile-time only statement, it is ignored during execution. The DO statement is executed next.
During the first iteration of the DO loop, the index variable i is set to 1. As a result, the array reference wt{i} becomes wt{1}. Because wt{1} refers to the first array element, Weight1, the value of Weight1 is converted from kilograms to pounds.
data hrd.convert; 
   set hrd.fitclass; 
   array wt{6} weight1-weight6; 
   do i=1 to 6; 
      wt{1}=wt{1}*2.2046; 
   end; 
run;
Program Data Vector
As the DATA step continues its DO loop iterations, the index variable i is changed from 1 to 2, 3, 4, 5, and 6, causing Weight1 through Weight6 to receive new values in the program data vector.
data hrd.convert; 
   set hrd.fitclass; 
   array wt{6} weight1-weight6; 
   do i=1 to 6; 
      wt{i}=wt{i}*2.2046; 
   end; 
run;

Using the DIM Function in an Iterative DO Statement

When using DO loops to process arrays, you can also use the DIM function to specify the TO clause of the iterative DO statement. For a one-dimensional array, specify the array name as the argument for the DIM function. The function returns the number of elements in the array.
Syntax, DIM function:
DIM(array-name)
array-name specifies the array.
In this example, dim(wt) returns a value of 6.
data hrd.convert; 
   set hrd.fitclass; 
   array wt{*} weight1-weight6; 
   do i=1 to dim(wt); 
      wt{i}=wt{i}*2.2046; 
   end; 
run;
When you use the DIM function, you do not have to re-specify the stop value of an iterative DO statement if you change the dimension of the array.
data hrd.convert; 
   set hrd.fitclass; 
   array wt{*} weight1-weight6; 
   do i=1 to dim(wt); 
      wt{i}=wt{i}*2.2046; 
   end; 
run;
data hrd.convert; 
   set hrd.fitclass; 
   array wt{*} weight1-weight10; 
   do i=1 to dim(wt); 
      wt{i}=wt{i}*2.2046; 
   end; 
run;
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.55.198