Expanding Your Use of One-Dimensional Arrays

Creating Character Columns with an Array

Suppose you need to create an array named States with 50 character columns.
By default, array columns are created as numeric and with the length of each column as 8. For character columns, you can specify a length other than 8 following the dollar sign ($).
The following example creates the States array with 50 character elements named State1-State50, each with a length of 12.
array States[50] $12 State1-State50;
If you do not specify a length, the default length of 8 is assigned to State1-State50.
array States[50] $ State1-State50;

Specifying Lower and Upper Bounds

When you specify an array with an asterisk (*), by default, the lower bound is 1, and the upper bound is the number of elements in the array. For example, both the following ARRAY statements have a lower bound of 1 and an upper bound of 6. The first element is referred to as element 1, and the second element as 2, and so on.
array years[6] yr2011-yr2016;
array years[*] yr2011-yr2016;
However, for some DATA step code it might be more efficient if the element number is referenced by another value. For example, it might be useful to reference the following element yr2011 as element 2011, the second as 2012, and so on. To do so, you can specify the lower and upper bounds using a colon between the two values.
array years[2011:2016] yr2011-yr2016;
do i=2011 to 2016;
When lower and upper bounds are specified, but the names of the variables are omitted, SAS does not create variable names with the bound values as the suffix. The following example creates the variables Years1, Years2, and so on, with the array. It does not create the variables Years2011, Years2012, and so on.
array years[2011:2016];

Assigning Initial Values to Arrays

It can be useful to assign initial values to elements of an array when you define the array.
   array goal[4] g1 g2 g3 g4 (initial values);
To assign initial values in an ARRAY statement:
  1. Place the values after the array elements.
  2. Specify one initial value for each corresponding array element.
  3. Separate each value with a comma or blank.
  4. Enclose the initial values in parentheses.
  5. Enclose each character value in quotation marks.
It is also possible to assign initial values to an array without specifying each array element. The following statement creates the variables Var1, Var2, Var3, and Var4, and assigns them initial values of 1, 2, 3, and 4:
   array Var[4] (1 2 3 4);

Example: Assigning Initial Values to Arrays

Suppose you want to compare the actual sales figures in the Certadv.Qsales data set to the sales goals for each sales representative at the beginning of the year. The sales goals are not recorded in Certadv.Qsales.
data work.report (drop=i);                       /*1*/
   set certadv.qsales;
   array sale[4] sales1-sales4;                  /*2*/
   array Goal[4] (9000 9300 9600 9900);          /*3*/
   array Achieved[4];                            /*4*/
   do i=1 to 4;                                  /*5*/
      achieved[i]=100*sale[i]/goal[i];
   end;
run;
proc print data=work.report noobs;
run;
1 The DATA step reads the Certadv.Qsales data set to create the Work.Report data set. You can drop the index variable from the new data set by adding a DROP= option to the DATA statement.
2 The first ARRAY statement creates an array, Sale. The four sales variables, Sales1 through Sales4, are being read from the input table Certadv.Qsales.
3 The second ARRAY statement creates the array, Goal, along with four variables Goal1 through Goal4, to provide the Sales goals for each quarter. The Goal array is assigned initial values of 9000, 9300, 9600, and 9900.
4 The third ARRAY statement creates the variables Achieved1 through Achieved4 to store the comparison of actual sales versus sales goals.
5 A DO loop executes four times to calculate the value of each element of the Achieved array (expressed as a percentage).
The output data set shows the variables that were read from Certadv.Qsales and the eight variables that were created with ARRAY statements.
Figure 11.3 PROC PRINT Results of SAS Data Work.Report
PROC PRINT Results of SAS Data Work.Report
Note: Variables to which initial values are assigned in an ARRAY statement are automatically retained.

Specifying Temporary Array Elements

Suppose you need to create an array with initial values. However, you know that you do not need to keep the values in the final table. You can specify the _TEMPORARY_ keyword in place of the array elements.
Temporary data elements do not have column names and can be referenced with the array reference. The temporary data elements do not appear in the output table and are also automatically retained. When you use the word _TEMPORARY_ there is no need to use a DROP statement to eliminate the unneeded columns in the final table.
When defining a temporary array, you must explicitly specify the number of elements within the array using a constant value within the array brackets. You cannot use an asterisk (*).
array salesquota[5] _temporary_;
Temporary arrays can either be numeric or character. You can create a temporary character array by including the dollar sign ($) after the array brackets.
array Names[20] $ _temporary_;
Temporary array elements are retained automatically between the iterations of the DATA step. You do not need to use the RETAIN statement. Temporary values within the array elements are never written to the output data set.
When referencing elements of a temporary array, use the array name and the number of elements (for example, SalesQuota[1] or Names[1]). Since there are no variables associated with the array, a reference to the columns SalesQuota1 or Names1 would not refer to the first element of the array.

Example: Rotating Data

Suppose you have the Certadv.QtrSales data set, which contains 25 rows of data. It contains sales data for the past five years for five different countries. For each country and year there are corresponding SalesQ columns. However, you are asked to create a new data set with the quarter sales values in one column.
Figure 11.4 Original Data versus Desired Output
Original Data Versus Desired Output
To rotate the data, use an array pointing to the four quarterly sales variables. Then use a DO loop to assign the appropriate quarter value to the new Sales column.
data work.yrsales;
   set certadv.qtrsales;
   array Yr[4] SalesQ1-SalesQ4;
   do Quarter=1 to 4;
      Sales=Yr[Quarter];
      output;
   end;
run;
Your output data contains the following variables: Country, Year, SalesQ1, SalesQ2, SalesQ3, SalesQ4, Quarter, and Sales. To limit the number of variables displayed in the output, use the VAR statement with PROC PRINT to display only Country, Year, Quarter, and Sales.
proc print data=work.yrsales;
   var Country Year Quarter Sales;
run;
The output data set contains 100 observations.
Output 11.3 PROC PRINT of Work.YrSales (partial output)
Partial Output: PROC PRINT of Work.YrSales
Last updated: October 16, 2019
..................Content has been hidden....................

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