So far, we have learned several ways to group existing variables into an array. We can also create new variables in an ARRAY statement by omitting the array elements from the statement. When our ARRAY statement fails to reference existing variables, SAS automatically creates new variables for us and assigns default names to them.
Example 19.7 Section
The following program again converts the average monthly Celsius temperatures in ten cities to average montly Fahrenheit temperatures. To do so, the already existing Celsius temperatures, jan, feb, ..., and dec, are grouped into an array called celsius, and the resulting Fahrenheit temperatures are stored in new variables janf, febf, ..., decf, which are grouped into an array called fahr:
DATA avgtemps; set avgcelsius; array celsius(12) jan feb mar apr may jun jul aug sep oct nov dec; array fahr(12) janf febf marf aprf mayf junf julf augf sepf octf novf decf; do i = 1 to 12; fahr(i) = 1.8*celsius(i) + 32; end; RUN; PROC PRINT data = avgtemps; title 'Average Monthly Temperatures'; id City; var jan janf feb febf mar marf; var apr aprf may mayf jun junf; var jul julf aug augf sep sepf; var oct octf nov novf dec decf; RUN;
The DATA step should look eerily similar to that of Example 7.6. The only thing that differs here is rather than writing over the Celsius temperatures, they are preserved by storing the calculated Fahrenheit temperatures in new variables called janf, febf, ..., and decf. The first ARRAY statement tells SAS to group the jan, feb, ..., dec variables in the avgcelsius data set into a one-dimensional array called celsius. The second ARRAY statement tells SAS to create twelve new variables called janf, febf, ..., and decf and to group them into an array called fahr. The DO loop processes through the twelve elements of the celsius array, converts the Celsius temperatures to Fahrenheit temperatures, and stores the results in the fahr array. The PRINT procedure then tells SAS to print the contents of the twelve Celsius temperatures and twelve Fahrenheit temperatures side-by-side. Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that the Celsius temperatures were properly converted to Fahrenheit temperatures.
Example 19.8 Section
The following program is identical to the previous program, except this time rather than naming the new variables grouped into the fahr array, we let SAS do the naming for us:
DATA avgtempsinF; set avgcelsius; array celsius(12) jan feb mar apr may jun jul aug sep oct nov dec; array fahr(12); do i = 1 to 12; fahr(i) = 1.8*celsius(i) + 32; end; RUN; PROC PRINT data = avgtempsinF; title 'Average Monthly Temperatures in Fahrenheit'; id City; var fahr1-fahr12; RUN;
Note that when we define the fahr array in the second ARRAY statement, we specify how many elements the fahr array should contain (12), but we do not specify any variables to group into the array. That tells SAS two things: i) we want to create twelve new variables, and ii) we want to leave the naming of the variables to SAS. In this situation, SAS creates default names by concatenating the array name and the numbers 1, 2, 3, and so on, up to the array dimension. Here, for example, SAS creates the names fahr1, fahr2, fahr3, ..., up to fahr12. That's why we refer to the Fahrenheit temperatures as fahr1 to fahr12 in the PRINT procedure's VAR statement. Launch and run the SAS program, and review the output from the PRINT procedure to convince yourself that the Celsius temperatures were again properly converted to Fahrenheit temperatures.