Using Arrays in SAS® Programming
DATA step programmers use arrays to simplify their code, which results in programs that are frequently more efficient and less error-prone. Consider, for example, a revenue-and-expense data set (Rev_Exp) that contains 24 monthly variables for a single year, 12 variables for revenues (Rev1–Rev12), and 12 variables for expenses (Exp1 - Exp12). To calculate the net income for each month, the SAS program needs 12 statements:
net_inc1 = rev1 - exp1; net_inc2 = rev2 - exp2; . . .eight other similar statements. . . net_inc11 = rev11 - exp11; net_inc12 = rev12 - exp12;
This method for calculating the net income is repetitive. What if the data set contains monthly data for 3 years, or even 10 years? As the amount of data increases, more statements are required to calculate net income for each month. As a result, the process becomes more tedious and error prone. Arrays can be used to perform these calculations with far fewer statements.
Keep these two points in mind as you explore the use of arrays:
- In nearly all cases, code that is written with arrays can also be written without arrays.
- Arrays simply provide an alternative method for referring to a variable rather than using the name of the variable.
- SAS Institute Inc. 2011. "Array Processing." In SAS® 9.3 Language Reference: Concepts. Cary, NC: SAS Institute Inc.
- SAS Institute Inc. 2011. "Array Reference Statement." In SAS® 9.3 Statements: Reference. Cary, NC: SAS Institute Inc.
- SAS Institute Inc. 2011 "ARRAY Statement." In SAS® 9.3 Statements: Reference. Cary, NC: SAS Institute Inc.