Processing Variables with Arrays |
Expanding Your Use of
Arrays |
For this example, assume you have the task of comparing the actual sales figures in the Finance.Qsales data set to the sales goals for each sales representative at the beginning of the year. The sales goals are not recorded in Finance.Qsales. |
Description of Finance.Qsales
Variable
Type
Length
SalesRep
char
8
Sales1
num
8
Sales2
num
8
Sales3
num
8
Sales4
num
8
The DATA step below reads the Finance.Qsales data set
to create the Finance.Report data set. The ARRAY statement
creates an array to process sales data for each quarter.
data finance.report; set finance.qsales; array sale{4} sales1-sales4; To compare the actual sales to the sales goals, you must create the variables for the sales goals. data finance.report; set finance.qsales; array sale{4} sales1-sales4; array Goal{4} (9000 9300 9600 9900);
A third ARRAY statement creates the variables data finance.report; set finance.qsales; array sale{4} sales1-sales4; array Goal{4} (9000 9300 9600 9900); array Achieved{4}; do i=1 to 4; achieved(i)=100*sale(i)/goal(i); end; run;
A DO loop executes four times to calculate the value of each element of the
data finance.report; set finance.qsales; array sale{4} sales1-sales4; array Goal{4} (9000 9300 9600 9900); array Achieved{4}; do i=1 to 4; achieved(i)=100*sale(i)/goal(i); end; run; Before submitting this DATA step, you can drop the index variable from the new data set by adding a DROP= option to the DATA statement. data finance.report(drop=i); set finance.qsales; array sale{4} sales1-sales4; array Goal{4} (9000 9300 9600 9900); array Achieved{4}; do i=1 to 4; achieved(i)=100*sale(i)/goal(i); end; run; The resulting data set contains the variables read from Finance.Qsales, plus the eight variables created with ARRAY statements. |
SAS Data Set Finance.Report
SalesRep
Sales1
Sales2
Sales3
Sales4
Goal1
Goal2
Britt
8400
8800
9300
9800
9000
9300
Fruchten
9500
9300
9800
8900
9000
9300
Goodyear
9150
9200
9650
11000
9000
9300
Goal3
Goal4
Achieved1
Achieved2
Achieved3
Achieved4
9600
9900
93.333
94.624
96.875
98.990
9600
9900
105.556
100.000
102.083
89.899
9600
9900
101.667
98.925
100.521
111.111
The variables Goal1 through Goal4 should not
be stored in the data set because they are only needed to calculate the values
of Achieved1 through Achieved4 . The next example
shows you how to create temporary array elements. |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.