19.3 - Creating Variables in an Array Statement

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 monthly 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;

Average Monthly Temperatures

City

jan

janf

feb

febf

mar

marf

apr

aprf

may

mayf

jun

junf

jul

julf

aug

augf

sep

sepf

oct

octf

nov

novf

dec

decf

State College, PA

-2

28.4

-2

28.4

2

35.6

8

46.4

14

57.2

19

66.2

21

69.8

20

68.0

16

60.8

10

50.0

4

39.2

-1

30.2

Miami, FL

20

68.0

20

68.0

22

71.6

23

73.4

26

78.8

27

80.6

28

82.4

28

82.4

27

80.6

26

78.8

23

73.4

20

68.0

St. Louis, MO

-1

30.2

1

33.8

6

42.8

13

55.4

18

64.4

23

73.4

26

78.8

25

77.0

21

69.8

15

59.0

7

44.6

1

33.8

New Orleans, LA

11

51.8

13

55.4

16

60.8

20

68.0

23

73.4

27

80.6

27

80.6

27

80.6

26

78.8

21

69.8

16

60.8

12

53.6

Madison, WI

-8

17.6

-5

23.0

0

32.0

7

44.6

14

57.2

19

66.2

22

71.6

20

68.0

16

60.8

10

50.0

2

35.6

-5

23.0

Houston, TX

10

50.0

12

53.6

16

60.8

20

68.0

23

73.4

27

80.6

28

82.4

28

82.4

26

78.8

21

69.8

16

60.8

12

53.6

Phoenix, AZ

12

53.6

14

57.2

16

60.8

21

69.8

26

78.8

31

87.8

33

91.4

32

89.6

30

86.0

23

73.4

16

60.8

12

53.6

Seattle, WA

5

41.0

6

42.8

7

44.6

10

50.0

13

55.4

16

60.8

18

64.4

18

64.4

16

60.8

12

53.6

8

46.4

6

42.8

San Francisco, CA

10

50.0

12

53.6

12

53.6

13

55.4

14

57.2

15

59.0

15

59.0

16

60.8

17

62.6

16

60.8

14

57.2

11

51.8

San Diego, CA

13

55.4

14

57.2

15

59.0

16

60.8

17

62.6

19

66.2

21

69.8

22

71.6

21

69.8

19

66.2

16

60.8

14

57.2

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;

Average Monthly Temperatures in Fahrenheit

City

fahr1

fahr2

fahr3

fahr4

fahr5

fahr6

fahr7

fahr8

fahr9

fahr10

fahr11

fahr12

State College, PA

28.4

28.4

35.6

46.4

57.2

66.2

69.8

68.0

60.8

50.0

39.2

30.2

Miami, FL

68.0

68.0

71.6

73.4

78.8

80.6

82.4

82.4

80.6

78.8

73.4

68.0

St. Louis, MO

30.2

33.8

42.8

55.4

64.4

73.4

78.8

77.0

69.8

59.0

44.6

33.8

New Orleans, LA

51.8

55.4

60.8

68.0

73.4

80.6

80.6

80.6

78.8

69.8

60.8

53.6

Madison, WI

17.6

23.0

32.0

44.6

57.2

66.2

71.6

68.0

60.8

50.0

35.6

23.0

Houston, TX

50.0

53.6

60.8

68.0

73.4

80.6

82.4

82.4

78.8

69.8

60.8

53.6

Phoenix, AZ

53.6

57.2

60.8

69.8

78.8

87.8

91.4

89.6

86.0

73.4

60.8

53.6

Seattle, WA

41.0

42.8

44.6

50.0

55.4

60.8

64.4

64.4

60.8

53.6

46.4

42.8

San Francisco, CA

50.0

53.6

53.6

55.4

57.2

59.0

59.0

60.8

62.6

60.8

57.2

51.8

San Diego, CA

55.4

57.2

59.0

60.8

62.6

66.2

69.8

71.6

69.8

66.2

60.8

57.2

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.