Populating an Array from a SAS Data Set

Overview

In the previous section, the wind chill lookup table was loaded into the WC array when the array was created. In many cases, you might prefer to load an array with values that are stored in a SAS data set rather than loading them in an ARRAY statement. Lookup values should be stored in a SAS data set when the following conditions are true:
  • there are too many values to initialize easily in the array
  • the values change frequently
  • the same values are used in many programs

Example

Suppose you want to compare the actual cargo revenue values in the SAS data set Sasuser.Monthsum to the target cargo revenue values in the SAS data set Sasuser.Ctargets.
Sasuser.Monthsum contains the actual cargo and passenger revenue figures for each month from 1997 through 1999.
Table 16.1 SAS Data Set Sasuser.Monthsum (first five observations of selected variables)
Obs
SaleMon
RevCargo
MonthNo
1
JAN1997
$171,520,869.10
1
2
JAN1998
$238,786,807.60
1
3
JAN1999
$280,350,393.00
1
4
FEB1997
$177,671,530.40
2
5
FEB1998
$215,959,695.50
2
The SAS data set Sasuser.Ctargets contains the target cargo revenue figures for each month from 1997 through 1999.
Table 16.2 SAS Data Set Sasuser.Ctargets
Obs
Year
Jan
Feb
Mar
Apr
May
Jun
1
1997
192284420
86376721
28526103
260386468
109975326
102833104
2
1998
108645734
147656369
202158055
41160707
264294440
267135485
3
1999
85730444
74168740
39955768
312654811
318149340
187270927
Obs
Jul
Aug
Sep
Oct
Nov
Dec
1
196728648
236996122
112413744
125401565
72551855
136042505
2
208694865
83456868
286846554
275721406
230488351
24901752
3
123394421
34273985
151565752
141528519
178043261
181668256
You want to create a new SAS data set, Work.Lookup1, that lists the actual and target values for each month. Work.Lookup1 should have the same structure as Sasuser.Monthsum: an observation for each month and year, as well as a new variable, Ctarget (target cargo revenues). The value of Ctarget is derived from the target values in Sasuser.Ctargets.
Table 16.3 SAS Data Set Work.Lookup1 (first five observations of selected variables)
Obs
SaleMon
RevCargo
Ctarget
1
JAN1997
$171,520,869.10
$192,284,420.00
2
JAN1998
$238,786,807.60
$108,645,734.00
3
JAN1999
$280,350,393.00
$85,730,444.00
4
FEB1997
$177,671,530.40
$86,376,721.00
5
FEB1998
$215,959,695.50
$147,656,369.00
Sasuser.Monthsum and Sasuser.Ctargets cannot be merged because they have different structures:
  • Sasuser.Monthsum has an observation for each month and year.
  • Sasuser.Ctargets has one column for each month and one observation for each year.
However, the data sets have two common factors: month and year. You can use a multidimensional array to match the actual values for each month and year in Sasuser.Monthsum with the target values for each month and year in Sasuser.Ctargets.

Creating an Array

The first step is to create an array to hold the values in the target data set, Sasuser.Ctargets. The array needs two dimensions: one for the year values and one for the month values. In the following program, the first ARRAY statement creates the two-dimensional array, Targets.
Remember that the dimension of an array does not have to range from one to the number of elements. You can specify a range for the values of the dimension when you define the array. In this case, the dimensions of the array are specified as 3 rows (one for each year: 1997, 1998, and 1999) and 12 columns (one for each month).
data work.lookup1;
  array Targets{1997:1999,12} _temporary_; 
  if _n_=1 then do i= 1 to 3;
	         set sasuser.ctargets;
	         array mnth{*} Jan--Dec;
	         do j=1 to dim(mnth);
	            targets{year,j}=mnth{j};
	         end;
  end;
  set sasuser.monthsum(keep=salemon revcargo monthno);
  year=input(substr(salemon,4),4.); 
  Ctarget=targets{year,monthno};
  format ctarget dollar15.2; 
run;
The following table represents the Targets array. Notice that the array is not populated. The next step is to load the array elements from Sasuser.Ctargets.
Table 16.4 Table Representation of Targets Array
1
2
3
4
5
6
7
8
9
10
11
12
1997
1998
1999
Note: The row dimension for the Targets array could have been specified using the value 3. Here is an example:
array Targets{3,12} _temporary_;
However, using the notation 1997:1999 simplifies the program by eliminating the need to map numeric values to the year values.

Loading the Array Elements

The Targets array needs to be loaded with the values in Sasuser.Ctargets. One method for accomplishing this task is to load the array within a DO loop.
Table 16.5 SAS Data Set Sasuser.Ctargets
Year
Jan
Feb
Mar
Apr
May
Jun
1997
192284420
86376721
28526103
260386468
109975326
102833104
1998
108645734
147656369
202158055
41160707
264294440
267135485
1999
85730444
74168740
39955768
312654811
318149340
187270927
Jul
Aug
Sep
Oct
Nov
Dec
196728648
236996122
112413744
125401565
72551855
136042505
208694865
83456868
286846554
275721406
230488351
24901752
123394421
34273985
151565752
141528519
178043261
181668256
The IF-THEN statement specifies that the Targets array is loaded only once, during the first iteration of the DATA step. The DO loop executes three times, once for each observation in Sasuser.Ctargets.
The ARRAY statement within the DO loop creates the Mnth array, which stores the values from Sasuser.Ctargets. The dimension of the Mnth array is specified using an asterisk, which enables SAS to automatically count the array elements.
Note: If you use an asterisk to specify the dimensions of an array, you must list the array elements. You cannot use an asterisk to specify an array's dimensions if the elements of the array are specified with the _TEMPORARY_ keyword.
The array elements Jan through Dec are listed using a double hyphen (- -). The double hyphen (- -) is used to read the specified values based on their positions in the PDV.
data work.lookup1;
  array Targets{1997:1999,12} _temporary_; 
  if _n_=1 then do i= 1 to 3;
	         set sasuser.ctargets;
	         array Mnth{*} Jan--Dec;
	         do j=1 to dim(mnth);
	            targets{year,j}=mnth{j};
	         end;
  end;
  set sasuser.monthsum(keep=salemon revcargo monthno);
  year=input(substr(salemon,4),4.); 
  Ctarget=targets{year,monthno};
  format ctarget dollar15.2;  
run;
The following table shows the values in the Mnth array after the first iteration of the DO loop.
Table 16.6 Table Representation of Mnth Array (after the first iteration of the DO loop)
Jan
Feb
Mar...
...Oct
Nov
Dec
192284420
86376721
260386468
125401565
72551855
136042505
Within the nested DO loop, the Targets array reference is matched to the Mnth array reference in order to populate the Targets array. The DIM function returns the number of elements in the Mnth array (in this case 12) and provides an ending point for the nested DO loop.
data work.lookup1;
  array Targets{1997:1999,12} _temporary_; 
  if _n_=1 then do i= 1 to 3;
	         set sasuser.ctargets;
	         array Mnth{*} Jan--Dec;
	         do j=1 to dim(mnth);
	            targets{year,j}=mnth{j};
	         end;
  end;
  set sasuser.monthsum(keep=salemon revcargo monthno);
  year=input(substr(salemon,4),4.); 
  Ctarget=targets{year,monthno}; 
  format ctarget dollar15.2; 
run;
Table 16.7 Table Representation of Mnth Array (after the second iteration of the DO loop)
Jan
Feb
Mar...
...Oct
Nov
Dec
108645734
147656369
202158055
275721406
230488351
24901752
Table 16.8 Table Representation of Mnth Array (after the third iteration of the DO loop)
Jan
Feb
Mar...
...Oct
Nov
Dec
85730444
74168740
39955768
141528519
178043261
181668256
Table 16.9 Table Representation of Populated Targets Array
1
2
3...
...10
11
12
1997
192284420
86376721
260386468
125401565
72551855
136042505
1998
108645734
147656369
202158055
275721406
230488351
24901752
1999
85730444
74168740
39955768
141528519
178043261
181668256
Note: The dimension of the Mnth array could also be specified using the numeric value 12. However, the asterisk notation enables the program to be more flexible. For example, using the asterisk, the program would not need to be edited if the target data set contained data for only 11 months. Remember that if you use an asterisk to count the array elements, you must list the array elements.

Reading the Actual Values

The last step is to read the actual values stored in Sasuser.Monthsum. Remember that you need to know the month and year values for each observation in order to locate the correct target revenue values.
Table 16.10 SAS Data Set Sasuser.Monthsum (first five observations of selected variables)
SaleMon
RevCargo
MonthNo
JAN1997
$171,520,869.10
1
JAN1998
$238,786,807.60
1
JAN1999
$280,350,393.00
1
FEB1997
$177,671,530.40
2
FEB1998
$215,959,695.50
2
The values for month are read from the numeric variable MonthNo. The year values are contained within the character variable SaleMon and can be extracted using the SUBSTR function. In this example, the SUBSTR function extracts four characters from SaleMon, starting at the fourth character. Note that the INPUT function is used to convert the value that is extracted from SaleMon from character to numeric in the assignment statement for Year. A numeric format performs the character to numeric conversion so the value of Year is used as an array reference.
The values of Ctarget are then looked up from the Targets array based on the values of Year and MonthNo.
data work.lookup1;
  array Targets{1997:1999,12} _temporary_; 
  if _n_=1 then do i= 1 to 3;
	         set sasuser.ctargets;
	         array Mnth{*} Jan--Dec;
	         do j=1 to dim(mnth);
	            targets{year,j}=mnth{j};
	         end;
  end;
  set sasuser.monthsum(keep=salemon revcargo monthno);
  year=input(substr(salemon,4),4.); 
  Ctarget=targets{year,monthno}; 
  format ctarget dollar15.2; 
run;
Table 16.11 Table Representation of Targets Array
1
2
3...
...10
11
12
1997
192284420
86376721
260386468
125401565
72551855
136042505
1998
108645734
147656369
202158055
275721406
230488351
24901752
1999
85730444
74168740
39955768
141528519
178043261
181668256
PROC PRINT output shows the new data set Work.Lookup1, which contains the actual cargo values (RevCargo) and the target cargo values (Ctarget).
Work.Lookup1 (first ten observations)
proc print data=work.lookup1 (obs=10);
   var salemon revcargo ctarget;
run;
proc print output
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.181.61