Beruflich Dokumente
Kultur Dokumente
Phil Spector
Statistical Computing Facility Department of Statistics University of California, Berkeley
http://www.stat.berkeley.edu/~spector
1
All variables in an array must have the same type (numeric or character). An array name cant have the same name as a variable. You must explicitly state the number of elements when using _temporary_; in other cases SAS gures it out from context, generating new variables if necessary.
3
A Simple Example
Suppose we have a data set with 10 variables, named x1,x2,. . .,x10. Whenever any of these variables has a value of 9, we wish to replace it with a missing value (.). data new; set old; array x x1-x10; do i=1 to dim(x); if x{i} = 9 then x{i} = .; end; run; Using dim(x) instead of a constant (10) eliminates the need to know the size of the array. Special variable lists (like first -- last or x:) can be very useful when setting up an array.
6
10 then year = .;
8
The goal is to create four observations for each original observation, one for each variable.
9
10
11
14
Multi-Dimensional Arrays
By providing multiple subscripts in the array statement, you can create multidimensional arrays. The number of variables in the array is the product of the ranges of their subscripts, and the variables in the array are stored by rows. If you declare a multi-dimensional array without specifying a list of variables (or the _temporary_ keyword), SAS will create variables with the array name followed by a single number ranging from 1 to the total number of elements in the array. To use the dim, hbound, or lbound functions with multi-dimensional arrays, use a second argument specifying the dimension you are interested in. (like dim(x,2)). (You can also use a function of the form dimn ().)
15
O6 1 0 1 1 0
O7 0 1 0 1 0
O8 0 1 0 0 1
O9 0 0 0 0 1
O10 0 0 0 0 0
Now we check to see which organisms were found for this observation, by storing the rst and last times that the organism was seen.
if last.id then do; do i=1 to 10; do j=1 to 5; if all{j,i} = 1 then do; if firsts{i} = 0 then firsts{i} = j; lasts{i} = j; end; end; end;
18
19