Processing Variables with Arrays |
Lesson
Summary![]() ![]() |
This page contains
|
I. Text Summary
To go to the page where a task, programming feature, or concept was presented, select a link. |
Purpose of SAS Arrays An array is a temporary grouping of variables under a single name. This can reduce the number of statements needed to process variables and can simplify the maintenance of DATA step programs. |
Defining an Array To group previously defined data set variables into an array, use an ARRAY statement that specifies the array's name, its dimension enclosed in braces, brackets, or parentheses, and the elements to include; for example: array sales{4}
qtr1 qtr2 qtr3 qtr4; |
Referencing Elements of an Array When you define an array in a DATA step, each element is assigned an index value. During execution, you can use an array reference to perform actions on specific array elements. When used in a DO loop, for example, the index variable of the iterative DO statement can reference each element of the array. |
The DIM Function When using DO loops to process arrays, you can also use the DIM function to specify the TO clause of the iterative DO statement. By using the DIM function, you do not have to respecify the stop value of a DO statement if you change the dimension of the array. |
Creating Variables with the ARRAY Statement If you don't specify array elements in an ARRAY statement, SAS software automatically creates the variables for you by concatenating the array name and the numbers 1, 2, 3 ... up to the array dimension. |
Creating an Array of Character Variables To create an array of character variables, add a dollar sign ( $ ) after the array dimension. By default, all character variables
created with an ARRAY statement are assigned a length of 8; you can specify
a different length after the dollar sign. |
Assigning Initial Values to Arrays To assign initial values in an ARRAY statement, place the values in parentheses after the array elements, specifying one initial value for each array element and separating each value with a comma or blank. To assign initial values to character variables, enclose each value in quotation marks. |
Creating Temporary Array Elements You can create temporary array elements for DATA step processing without creating additional variables. Just specify _TEMPORARY_ after the array name and dimension. This is useful when the array is needed only to perform a calculation. |
Multidimensional Arrays To define a multidimensional array, specify the number of elements in each dimension, separated by a comma. For example, array new(3,4)
x1-x12; |
Referencing Elements of a Two-Dimensional Array Multidimensional arrays are typically used with nested DO loops. If a DO loop depends on a two-dimensional array, you can reference any element within the array by specifying the two dimensions.
|
II. Syntax
To go to the page where a statement or option was presented, select a link.
|
III. Sample Programs data report; set master.temps; array wkday(7) mon tue wed thr fri sat sun; do i=1 to 7; wkday(i)=5*(wkday(i)-32)/9; end; run; data hrd.convert; set hrd.fitclass; array wt(6) weight1-weight6; do i=1 to dim(wt); wt(i)=wt(i)*2.2046; end; run; data hrd.diff; set hrd.convert; array wt(6) weight1-weight6; array WgtDiff(5); do i=1 to 5; WgtDiff(i)=wt(i+1)-wt(i); end; run; data finance.report; set finance.qsales; array sale{4} sales1-sales4; array Goal{4} _temporary_ (9000 9300 9600 9900); array Achieved{4}; do i=1 to 4; achieved(i)=100*sale(i)/goal(i); end; run; data finance.quarters(drop=i j); set finance.monthly; array m(4,3) month1-month12; array Qtr(4); do i=1 to 4; qtr(i=0); do j=1 to 3; qtr(i)+m(i,j); end; end; run;
|
IV. Points to Remember
|
![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.