Multiple Array, How to use multiple Array, How to initialize multiple array
Multiple arrays to input and process data
In this example, we are illustrating the use of more than one array to input and process data, as well as slightly more advanced used of do-loops to process data. We will create four groups of variables: age, dv, dvn and dvm. Each of these groups of variables will have five variables in it. The groups dv, dvn and dvm are groups of dependent variables, and age is considered to be an independent variable. Unlike the previous two examples, we will do the manipulations that we want in the same data step that we use to input the data. First, if age is less than 10, we would like to add 100 to the first group of dependent variables (dv). We would like to add 200 to the second group of dependent variables (dvn), and add 300 to the third set of dependent variables (dvm). Because the amount that we want to add to the scores increments (by 100) with the index variable i, we will multiple i by 100 and add it to the scores for each dependent variable. Also, for the first group of dependent variables (dv), we would like to divide scores by 50 if age is greater than or equal to 12. For the second group of dependent variables (dvn), we would like to divide scores by two for the same age condition. To specify that just one group should be processed, we list the number of the group to be processed in the array statement. Notice also that dividing is handled differently than adding: we need to use an equality (the = sign) to do the division. Finally, notice that the first do-loop is not closed until all processing has been finished, because the index variable k is used throughout the processing.
data ages;
array age(5) age1 - age5;
array dv(3, 5) dv1 - dv5 dvn1 - dvn5 dvm1 - dvm5;
input age1 - age5 / dv1 - dv5 / dvn1 - dvn5 / dvm1 - dvm5;
do k = 1 to 5;
if (age(k) < 10) then
do i = 1 to 3;
dv(i, k) + i*100;
end;
if (age(k) >= 12) then do;
dv(1, k) = dv(1, k) / 50;
dv(2, k) = dv(2, k) / 2;
end;
end;
cards;
9 15 8 21 2 3
65 98 32 64 64
65 67 74 85 96
50 21 63 41 32
10 5 6 8 12 5
45 78 78 98 65
20 23 30 36 35
34 32 11 12 88
1 9 6 5 12 7
85 74 85 25 45
98 65 31 21 32
65 98 32 64 64
;
run;
proc print data = ages;
run;
a a a a a d d d d d d d d d d
O g g g g g d d d d d v v v v v v v v v v
b e e e e e v v v v v n n n n n m m m m m
s 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
1 9 15 8 21 2 165 1.96 132 1.28 164.0 265 33.5 274 42.5 296.0 350 21 363 41 332
2 10 5 6 8 12 45 178.00 178 198.00 1.3 20 223.0 230 236.0 17.5 34 332 311 312 88
3 1 9 6 5 12 185 174.00 185 125.00 0.9 298 265.0 231 221.0 16.0 365 398 332 364 64