Using Multidimensional Arrays

Review of the Multidimensional Array Statement

When a lookup operation depends on more than one ordinal numeric key, you can use a multidimensional array. You use an ARRAY statement to create an array. The ARRAY statement defines a set of elements that you process as a group.
General form, multidimensional ARRAY statement:
ARRAY array-name {rows,cols,...} <$> <length>
<array-elements> <(initial values)>;
Here is an explanation of the syntax:
array-name
names the array.
rows
specifies the number of array elements in the row dimension.
cols
specifies the number of array elements in the column dimension.
array-elements
names the variables that make up the array.
initial values
specifies initial values for the corresponding elements in the array, separated by commas or spaces.
Note: The keyword _TEMPORARY_ might be used instead of array-elements to avoid creating new data set variables.
When you work with arrays, remember the following:
  • the name of the array must be a SAS name that is not the name of a SAS function or variable in the same DATA step
  • the variables listed as array elements must all be the same type (either all numeric or all character)
  • the initial values that are specified can be numbers or character strings. You must enclose all character strings in quotation marks
Note: If you use the _TEMPORARY_ keyword in an array statement, remember that temporary data elements behave like DATA step variables with the following exceptions:
  • They do not have names. Refer to temporary data elements by the array name and dimension.
  • They do not appear in the output data set.
  • You cannot use the special subscript asterisk (*) to refer to all the elements.
  • Temporary data element values are always automatically retained, rather than being reset to missing at the beginning of the next iteration of the DATA step.

Example

Suppose you need to determine the wind chill values for the flights that are represented in the SAS data set Sasuser.Flights. The data set contains three variables: Flight (the flight number), Temp (the average outdoor temperature during the flight), and Wspeed (the average wind speed during the flight).
Figure 16.1 SAS Data Set Sasuser.Flights
Obs, Flight, Temp, Wspeed
Wind chill values are derived from the air temperature and wind speed as shown in the following wind chill lookup table. To determine the wind chill for each flight, you can create a multidimensional array that stores the wind chill values shown in the table. You can then match the values of Temp and Wspeed with the wind chill values that are stored in the array.
Figure 16.2 Temperature (in degrees Fahrenheit)
Wind speed
In the following program, the ARRAY statement creates the two-dimensional array WC and specifies the dimensions of the array: four rows and two columns. No variables are created from the array because the keyword _TEMPORARY_ is used. The initial values that are specified correspond to the values in the wind chill lookup table. For this example, only the values in the first two columns and four rows in the wind chill lookup table are included in the array.
data work.wndchill (drop=column row);
  array WC{4,2} _temporary_
        (-22,-16,-28,-22,-32,-26,-35,-29);
  set sasuser.flights;
  row=round(wspeed,5)/5;
  column=(round(temp,5)/5)+3;
  WindChill=wc{row,column};
run;
Figure 16.3 Temperature (in degrees Fahrenheit)
Wind speed
The value of WindChill for each flight is determined by referencing the array based on the values of Wspeed and Temp in the Sasuser.Flights data set. The row number for the array reference is determined by the value of Wspeed. The column number for the array reference is determined by the value of Temp.
Table Representation of the WC Array
data work.wndchill (drop = column row);
  array WC{4,2} _temporary_ 
        (-22,-16,-28,-22,-32,-26,-35,-29);
  set sasuser.flights;
  row = round(wspeed,5)/5;
  column = (round(temp,5)/5)+3;
  WindChill= wc{row,column};
run;
Table Representation of the WC Array
The rounding unit for the value of Wspeed is 5 because the values for wind speed in the wind chill table are rounded to every 5 miles-per-hour. Wspeed is then divided by 5 to derive the row number for the array reference.
Like the value for Wspeed, the value of Temp is rounded to the nearest 5, and then divided by 5. The offset of 3 is added to the value because the third column in the wind chill lookup table represents 0 degrees.
data work.wndchill (drop = column row);
  array WC{4,2} _temporary_ 
        (-22,-16,-28,-22,-32,-26,-35,-29);
  set sasuser.flights;
  row = round(wspeed,5)/5;
  column = (round(temp,5)/5)+3;
  WindChill= wc{row,column};
run;
PROC PRINT output shows the completed data set.
proc print data=work.wndchill;
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
18.119.128.113