This example illustrates two ways of operating on the same input variables and producing the same tabular report. To simplify the example, no report enhancements are shown.
The manager of a hotel chain wants a report that shows the number of bookings at its hotels in each of four cities, the total number of bookings in the current quarter, and the percentage of the total coming from each location for each quarter of the year. Input observations contain the following variables: REPTDATE (report date), LA (number of bookings in Los Angeles), ATL (number of bookings in Atlanta), CH (number of bookings in Chicago), and NY (number of bookings in New York).
The following DATA step creates the SAS data set BOOKINGS:
data bookings; input reptdate date9. la atl ch ny; datalines; 01JAN1989 100 110 120 130 01FEB1989 140 150 160 170 01MAR1989 180 190 200 210 01APR1989 220 230 240 250 01MAY1989 260 270 280 290 01JUN1989 300 310 320 330 01JUL1989 340 350 360 370 01AUG1989 380 390 400 410 01SEP1989 420 430 440 450 01OCT1989 460 470 480 490 01NOV1989 500 510 520 530 01DEC1989 540 550 560 570 ;
The following PROC COMPUTAB statements select columns by setting _COL_ to an appropriate value. The PCT1, PCT2, PCT3, and PCT4 columns represent the percentage contributed by each city to the total for the quarter. These statements produce Output 9.1.1.
proc computab data=bookings cspace=1 cwidth=6; columns qtr1 pct1 qtr2 pct2 qtr3 pct3 qtr4 pct4; columns qtr1-qtr4 / format=6.; columns pct1-pct4 / format=6.2; rows la atl ch ny total; /* column selection */ _col_ = qtr( reptdate ) * 2 - 1; /* copy qtr column values temporarily into pct columns */ colcopy: pct1 = qtr1; pct2 = qtr2; pct3 = qtr3; pct4 = qtr4; /* calculate total row for all columns */ /* calculate percentages for all rows in pct columns only */ rowcalc: total = la + atl + ch + ny; if mod( _col_, 2 ) = 0 then do; la = la / total * 100; atl = atl / total * 100; ch = ch / total * 100; ny = ny / total * 100; total = 100; end; run;
Using the same input data, the next set of statements shows the usefulness of arrays in allowing PROC COMPUTAB to work in two directions at once. Arrays in larger programs can both reduce the amount of program source code and simplify otherwise complex methods of referring to rows and columns. The same report as in Output 9.1.1 is produced.
proc computab data=bookings cspace=1 cwidth=6; columns qtr1 pct1 qtr2 pct2 qtr3 pct3 qtr4 pct4; columns qtr1-qtr4 / format=6.; columns pct1-pct4 / format=6.2; rows la atl ch ny total; array pct[4] pct1-pct4; array qt[4] qtr1-qtr4; array rowlist[5] la atl ch ny total; /* column selection */ _col_ = qtr(reptdate) * 2 - 1; /* copy qtr column values temporarily into pct columns */ colcopy: do i = 1 to 4; pct[i] = qt[i]; end; /* calculate total row for all columns */ /* calculate percentages for all rows in pct columns only */ rowcalc: total = la + atl + ch + ny; if mod(_col_,2) = 0 then do i = 1 to 5; rowlist[i] = rowlist[i] / total * 100; end; run;