Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional...

70
Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two- dimensional arrays Perform an action on array elements Create new variables with ARRAY statement Assign initial values to array elements Create temporary elements with an ARRAY statement

Transcript of Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional...

Page 1: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Chapter 16Processing Variables with Arrays

Objectives• Group variables into one- and two-dimensional arrays• Perform an action on array elements• Create new variables with ARRAY statement• Assign initial values to array elements• Create temporary elements with an ARRAY statement

Page 2: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

2

Array Processing

You can use arrays to simplify programs that– perform repetitive calculations– create many variables with the same attributes– read data– rotate SAS data sets by making variables into

observations or observations into variables– compare variables– perform a table lookup.

Page 3: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

3

Performing Repetitive CalculationsEmployees contribute an amount to charity every quarter. The SAS data set mylib.donate contains contribution data for each employee. The employer supplements each contribution by 25 percent. Calculate each employee’s quarterly contribution including the company supplement.

Partial Listing of mylib.donate

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Page 4: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

4

Performing Repetitive Calculations

data charity; set mylib.donate; Qtr1=Qtr1*1.25; Qtr2=Qtr2*1.25; Qtr3=Qtr3*1.25; Qtr4=Qtr4*1.25;run;

proc print data=charity noobs;run;

The following program does the purpose without using an ARRAY

Page 5: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

5

Partial PROC PRINT Output

What if you want to similarly modify 52 weeks of data

stored in Week1 through Week52?

Performing Repetitive Calculations

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 15.00 41.25 27.50 .E00367 43.75 60.00 50.00 37.50E00441 . 78.75 111.25 112.50E00587 20.00 23.75 37.50 36.25E00598 5.00 10.00 7.50 1.25

Page 6: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

6

What Is a SAS Array?

A SAS array – is a temporary grouping of SAS variables that

are arranged in a particular order– is identified by an array name – exists only for the duration of the current DATA

step – is not a variable.

Page 7: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

7

What Is a SAS Array?

Each value in an array is– called an element– identified by a subscript that represents the

position of the element in the array.

When you use an array reference, the corresponding value is substituted for the reference.

Page 8: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

8

What Is a SAS Array?

...

D

ID Qtr4Qtr2 Qtr3Qtr1

CONTRIBCONTRIB

Firstelement

Secondelement

Thirdelement

Fourthelement

Array name

Page 9: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

9

What Is a SAS Array?

ID Qtr4Qtr2 Qtr3Qtr1

CONTRIBCONTRIB

Firstelement

Secondelement

Thirdelement

Fourthelement

Array references

CONTRIB{1} CONTRIB{2} CONTRIB{3} CONTRIB{4}

Array name

Page 10: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

10

The ARRAY Statement

The ARRAY statement defines the elements in an array. These elements can be processed as a group. You refer to elements of the array by the array name and subscript.

ARRAY array-name {array-subscript} <$> <length> <array-elements> <(initial-value-list)>;

ARRAY array-name {array-subscript} <$> <length> <array-elements> <(initial-value-list)>;

Page 11: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

11

The ARRAY Statement

The ARRAY statement– must contain all numeric or all character

elements– must be used to define an array before the array

name can be referenced– creates variables if they do not already exist in the

PDV

Page 12: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Some warnings when using Arrays

• Do not give an array the same name as a variable name in the same DATA step.

• Avoid using the SAS function name as an array name; although the array will still be correct, but, you can not use it as a SAS function in the same Data Step, a warning message will be in the SAS Log.

• Can not use array name in LABEL, FORMAT, DROP, KEEP, or LENGTH statements.

• Arrays do not become part of the output data set. They are temporary names.

Page 13: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Creating an One-Dimensional ArrayAn example of using one-dimensional array to reduce the # of program

statements. The following program convert Fanrenheit to Celsius temperature for each week day without using Array:

Data temperature_convert; set Fahrenheit;Mon = 5*(Mon-32)/9;Tue=5*(Tue-32)/9;Wed=5*(Wed-32)/9;Thr=5*(Thr-32)/9;Fri=5*(Fri-32)/9;Sat=5*(Sat-32)/9;Sun=5*(Sun-32)/9;Run;

Page 14: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

The following program convert Fanrenheit to Celsius temperature for each

week day with an Array:

Data temperature_convert (drop=i);Set Fahrenheit;Array wkday{7} mon tue wed thr fri sat sun;Array celtemp[7] cmon ctue cwed cthr cfri csat csun;Do i = 1 to 7;celtemp{i} = 5*(wkday{i}-32)/9;End;Run;

NOTE: • The array name is wkday• # of elements defined in the array is 7.• The seven elements are the variables mon, tue, etc.• The use of the array in the program is by the DO loop.• The index in the DO loop is a new variable created in the program. It should

dropped , unless it will be used for other purpose in the same Data step.

Page 15: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

15

Defining an ArrayWrite an ARRAY statement that defines the four quarterly contribution variables as elements of an array.

array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4;

Firstelement

Secondelement

Thirdelement

Fourthelement

ID Qtr4Qtr2 Qtr3Qtr1

CONTRIBCONTRIB

Page 16: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

16

Defining an Array

Variables that are elements of an array do not need to have similar, related, or numbered names.

array Contrib2{4} Q1 Qrtr2 ThrdQ Qtr4;

Qtr4Qrtr2 ThrdQQ1

Firstelement

Secondelement

Thirdelement

Fourthelement

ID

CONTRIB2CONTRIB2

Page 17: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Specifying Array-subscript

There are different ways to specify array-subscript in addition to specify the size of the array.

Ex.:• Specify a range of values as dimension:

Array sales{05:09} mon05 mon06 mon07 mon08 mon09;• Use asterisk (*) as dimension. SAS determine the dimension of

array by counting the number of elements.Array contrib{*} qtr1 qtr2 qtr3 qtr4;

• You can use { } , [ ] , or ( ) to enclose dimension:array sales[4] qtr1 qtr2 qtr3 qtr4;array sales (4) qtr1 qtr2 qtr3 qtr4;

Page 18: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Specifying Array Elements

Array sales {4} qtr1 qtr2 qtr3 qtr4;This array has four elements, which are defined by the four variables

qtr1 atr2 qtr3 and qtr4.

• It can be simplified by using qtr1 – qtr4:Array sales[4] qtr1 – qtr4;

• Since array elements are a list of variables, one can use the following as array elements just like we describe a variable list:

A numbered range of variables Var1 – Varn The list of variables from A to B A - - BAll numeric variables _NUMERIC_All character variables _CHARACTER_All variables _ALL_

• NOTE: When _ALL_ is used, by default, all variables must be either numeric or character. It can not be a mixed list of variables.

Page 19: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Some examples of Array StatementsArray sales (6) mon7 mon8 mon9 mon10 mon11 mon12;Array sales {7:12} mon7 mon8 mon9 mon10 mon11 mon12;Array sales(*) july august sept oct nov dec;Array sales{*} july - - dec; /*NOTE: use A - - B for the entire list of variables

from A to B */ Array sales [*] mon7-mon12;Array sales (*) _numeric_ ;Array names{*} _character_;

If the entire list of variables are either all numeric or all character, one can specify the Array statement as:

Array names {*} _ALL_;

NOTE: the asterisk is used as the dimension if the # of the elements are not known. SAS will count the # of elements in the list.

Page 20: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

In some situations, we do not have pre-defined variable list in the Data Step for the array.

Can we define an Array statement without providing the array elements?

The answer is YES. SAS will create a list of default names for the elements.

General Syntax is : Array a_name {dimension};

SAS creates a default list of variables as:a_name1, a_name2, … , a_namen , where n is the array dimension.

Ex: Array wtdif{4}; creates four default variable list named as

wtdif1 wtdif2 wtdif3 wtdif4;

Page 21: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

21

Processing an Array

Array processing often occurs within DO loops. An iterative DO loop that processes an array typically has the following form:

To execute the loop as many times as there are elements in the array, specify that the values of index-variable range from 1 to number-of-elements-in-array.

DO index-variable=1 TO number-of-elements-in-array; additional SAS statements using array-name{index-variable}…END;

DO index-variable=1 TO number-of-elements-in-array; additional SAS statements using array-name{index-variable}…END;

Page 22: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

22

CONTRIB{i}CONTRIB{i}

Processing an Arrayarray Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4;do i=1 to 4; Contrib{i}=Contrib{i}*1.25;end;

Qtr4

Qtr2

Qtr3

Qtr1

...

Firstelement

Secondelement

Thirdelement

Fourthelement

Page 23: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

23

CONTRIB{i}CONTRIB{i}

Processing an Arrayarray Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4;do i=1 to 4; Contrib{i}=Contrib{i}*1.25;end;

Qtr4

Qtr2

Qtr3

Qtr1

1

Value of index

variable i

...

Firstelement

Secondelement

Thirdelement

Fourthelement

Page 24: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

24

CONTRIB{i}CONTRIB{i}

4

CONTRIB{4}

3

CONTRIB{3}

2

CONTRIB{2}

Processing an Arrayarray Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4;do i=1 to 4; Contrib{i}=Contrib{i}*1.25;end;

Qtr4

Qtr2

Qtr3

Qtr1

1

Value of index

variable i

CONTRIB{1}

array reference

Firstelement

Secondelement

Thirdelement

Fourthelement

Page 25: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Use the DIM(array_name) function to call out the dimension of an array when array dimension is not specified in the array statement.

When the dimension is not specified in Array statement, SAS determines the # of dimensions by counting the # of elements.

When processing the array in the DO loop, we need to specify the dimension to be processed.

Since SAS has already counted the dimension, all we need to do is to call out this dimension.

The following example convert distances from six cities to Mt. Pleasant from MILES to Kilometers.

Array distance(*) Dist1 – Dist6;Do k = 1 to DIM(distance);Dist{k} = Dist{k} * 1.6 ;End; run;

Page 26: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

27

Performing Repetitive Calculations

When i=1

...

data charity(drop=i); set mylib.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do i=1 to 4; Contrib{i}=Contrib{i}*1.25; end; run;

Contrib{1}=Contrib{1}*1.25;

Qtr1=Qtr1*1.25;

Page 27: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

28

Performing Repetitive Calculations

When i=2

...

data charity(drop=i); set mylib.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do i=1 to 4; Contrib{i}=Contrib{i}*1.25; end; run;

Contrib{2}=Contrib{2}*1.25;

Qtr2=Qtr2*1.25;

Page 28: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

29

Performing Repetitive Calculations

When i=3

...

data charity(drop=i); set mylib.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do i=1 to 4; Contrib{i}=Contrib{i}*1.25; end; run;

Contrib{3}=Contrib{3}*1.25;

Qtr3=Qtr3*1.25;

Page 29: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

30

Performing Repetitive Calculations

When i=4

Qtr4=Qtr4*1.25;

data charity(drop=i); set mylib.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do i=1 to 4; Contrib{i}=Contrib{i}*1.25; end; run;

Contrib{4}=Contrib{4}*1.25;

Page 30: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

31

Performing Repetitive Calculations

Partial PROC PRINT Output

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 15.00 41.25 27.50 .E00367 43.75 60.00 50.00 37.50E00441 . 78.75 111.25 112.50E00587 20.00 23.75 37.50 36.25E00598 5.00 10.00 7.50 1.25

proc print data=charity noobs;run;

Page 31: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

32

Creating Variables with ArraysCalculate the percentage that each quarter’s contribution represents of the employee’s total annual contribution. Base the percentage only on the employee’s actual contribution and ignore the company contributions.

Partial Listing of mylib.donate

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Page 32: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

33

Creating Variables with Arraysdata percent(drop=i); set mylib.donate; Total=sum(of Qtr1-Qtr4); array Contrib{4} Qtr1-Qtr4; array Percent{4}; do i=1 to 4; Percent{i}=Contrib{i}/Total; end; run;

The second ARRAY statement automatically creates four numeric variables:

Percent1, Percent2, Percent3, Percent4.

Page 33: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

34

Creating Variables with Arrays

ID Percent1 Percent2 Percent3 Percent4

E00224 18% 49% 33% .E00367 23% 31% 26% 20%E00441 . 26% 37% 37%E00587 17% 20% 32% 31%E00598 21% 42% 32% 5%

proc print data=percent noobs; var ID Percent1-Percent4; format Percent1-Percent4 percent6.;run;

Partial PROC PRINT Output

NOTE: percent6. is a display format for displaying percentage with % sign.

Page 34: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

35

Creating Variables with ArraysCalculate the difference in each employee’s actual contribution from one quarter to the next.

Partial Listing of mylib.donate

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

...

Firstdifference

Page 35: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

36

Creating Variables with Arrays

Calculate the difference in each employee’s actual contribution from one quarter to the next.

Partial Listing of mylib.donate

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

...

Firstdifference

Seconddifference

Page 36: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

37

Creating Variables with Arrays

Calculate the difference in each employee’s actual contribution from one quarter to the next. Partial Listing of mylib.donate

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Thirddifference

Firstdifference

Seconddifference

Page 37: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

38

Creating Variables with Arrays

data change(drop=i); set mylib.donate; array Contrib{4} Qtr1-Qtr4; array Diff{3}; do i=1 to 3; Diff{i}=Contrib{i+1}-Contrib{i}; end; run;

Page 38: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

39

Creating Variables with Arrays

ID Diff1 Diff2 Diff3

E00224 21 -11 .E00367 13 -8 -10E00441 . 26 1E00587 3 11 -1E00598 4 -2 -5

proc print data=change noobs; var ID Diff1-Diff3; run;

Partial PROC PRINT Output

Page 39: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Assigning initial values in ARRAY statementThere are situations where initial values will be assigned to array

quarter. The difference between the actual sales and the goal will be computed each quarter.

Using array to assign the initial values using the following syntax:

ARRAY array_name(dim) variable names (initial values);

Example:ARRAY sales[4] sale1 – sale4;ARRAY goals[4] goal1 – goal4 (5000 6000 7500 9000);

NOTE: Each variable is assigned an initial value in the same order of the sequence of the initial values.

Page 40: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Assigning initial values in an ARRAY without creating new variables in the SAS

data setBy default, when array is defined, the elements are either provided or will be created by the ARRAY using array_name1, array_name2 , etc.

Ex: ARRAY sales(4) sale1 – sale4;ARRAY diff{4} ; creates four variables diff1, diff2, diff3, diff4.ARRAY goal{4} (5000 6000 7500 9000);Will creates four variables goal1 =5000, goal2=6000, goal3=7500 and

goal4=9000 in the SAS data set.Since these will be used to determine the difference between goals

and sales, there is no need to add these variables to SAS data set.We can use _TEMPORARY_ in ARRAY statement to create temporary

variables without adding to SAS data set in array:ARRAY array_name{dim} _Temporary_ (initial values);EX: ARRAY goal[4] _Temporary_ (5000 6000 7500 9000);

Page 41: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

42

Assigning Initial Values in an Array Statement

Determine the difference between employee contributions and last year’s average quarterly goals of $10, $15, $5, and $10 per employee.

data compare(drop=i Goal1-Goal4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do i=1 to 4; Diff{i}=Contrib{i}-Goal{i}; end;run;

If array consists of constants, the parenthesis is needed for the constants.

Page 42: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

43

Assigning Initial Values

ID Diff1 Diff2 Diff3 Diff4

E00224 2 18 17 .E00367 25 33 35 20E00441 . 48 84 80E00587 6 4 25 19E00598 -6 -7 1 -9

proc print data=compare noobs; var ID Diff1 Diff2 Diff3 Diff4;run;

Partial PROC PRINT Output

Page 43: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

44 ...

ID Qtr3Qtr1 Qtr2 Qtr4

PDV

data compare(drop=i Goal1-Goal4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do i=1 to 4; Diff{i}=Contrib{i}-Goal{i}; end;run;

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Creating Variables with Arrays: Compilation

Page 44: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

45 ...

ID Qtr3Qtr1 Qtr2 Diff1 Diff2Qtr4

PDV

Diff3 Diff4

data compare(drop=i Goal1-Goal4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do i=1 to 4; Diff{i}=Contrib{i}- Goal{i}; end;run;

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Creating Variables with Arrays: Compilation

Page 45: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

46 ...

ID Qtr3Qtr1 Qtr2 Diff1 Diff2Qtr4

PDV

1051510

Diff3 Goal2Diff4 Goal1 Goal4Goal3

data compare(drop=i Goal1-Goal4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do i=1 to 4; Diff{i}=Contrib{i}- Goal{i}; end;run;

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Creating Variables with Arrays: Compilation

Page 46: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

47 ...

ID Qtr3Qtr1 Qtr2 Diff1 Diff2Qtr4

PDV

1051510

Diff3 Goal2Diff4 Goal1 Goal4 iGoal3

data compare(drop=i Goal1-Goal4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do i=1 to 4; Diff{i}=Contrib{i}- Goal{i}; end;run;

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Creating Variables with Arrays: Compilation

Page 47: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

48

ID Qtr3Qtr1 Qtr2 Diff1 Diff2Qtr4

PDV

1051510

Diff3 Goal2Diff4 Goal1 Goal4 iGoal3D D DD D

data compare(drop=i Goal1-Goal4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do i=1 to 4; Diff{i}=Contrib{i}- Goal{i}; end;run;

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Creating Variables with Arrays: Compilation

Page 48: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

49

Performing a Table Lookup without creating new variable lists in Array Statement

You can use the keyword _TEMPORARY_ instead of specifying variable names when you create an array to define temporary array elements. data compare(drop=i);

set mylib.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} _temporary_ (10,15,5,10); do i=1 to 4; Diff{i}=Contrib{i}-Goal{i}; end;run;

When array is created, the corresponding variables are created unless they are dropped or assigned as _TEMPORARY_

In this example, SAS creates Diff1-Diff4 new variablesIf we did not use _temporary_, then SAS would have created Goal1 – Goal4 as well.

Page 49: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Defining SAS Array with Character Variables

We have discussed defining and processing arrays with numeric variables. SAS array can also handle character variables. The Array statement has the following syntax:

Array Array_name{dim} $ <length> <list of character variables >;

• $ is required for character arrays.• By default, all character variables created by SAS array have the

length 8. specifying length to overwrite the length. The length specified is assigned to all character variables created by ARRAY statement.

Ex:ARRAY Employ{4} $ Emp90 Emp95 emp2000 emp2005; ARRAY Employ(4) $ ; creates employ1 – employ4, each has length 8.ARRAY Employ[4] $ 30; creates employ1 – employ4, each with length 30.

Page 50: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

51

Rotating a SAS Data SetRotating, or transposing, a SAS data set can be accomplished by using array processing. When a data set is rotated, the values of an observation in the input data set become values of a variable in the output data set.

Partial Listing of mylib.donate

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Page 51: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

52

ID Qtr Amount

Partial Listing of rotate

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

E00224 1 12E00224 2 33E00224 3 22E00224 4 .E00367 1 35E00367 2 48E00367 3 40E00367 4 30

Rotating a SAS Data Set

Page 52: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

53

Rotating a SAS Data Setdata rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

1) The variable Qtr is created to be 1,2,3, 42) The variable Amount is created to for the corresponding amount is

each quarter. 3) OUTPUT explicitly write each quarter amount as an observation. 4) DROP is used to drop un-needed variables.

Page 53: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

54

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

ID Qtr Amountrotate data set

Rotating a SAS Data Set: Compilation

PDVID Qtr3Qtr1 Qtr2 Qtr AmountQtr4

D D D D

Page 54: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

55

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

Rotating a SAS Data Set: Execution

Initialize PDV to missing

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

PDV

......

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

Page 55: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

56

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

...223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

PDV

Page 56: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

57

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

.1.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

PDV

Page 57: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

58

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

121.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Amount=Contrib{1};

PDV

Page 58: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

59

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

121.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

ID Qtr Amount

E00224 1 12

rotate data set

PDV

Page 59: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

60

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

332.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Amount=Contrib{2};

PDV

Page 60: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

61

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

332.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

ID Qtr Amount

E00224 1 12 E00224 2 33

PDV

rotate data set

Page 61: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

62

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

2233223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Amount=Contrib{3};

PDV

Page 62: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

63

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

223.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22

PDV

rotate data set

Page 63: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

64

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

.4.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

Amount=Contrib{4};

PDV

Page 64: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

65

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

...

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

.4.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D D

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22 E00224 4 .

PDV

rotate data set

Page 65: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

66

data rotate(drop=Qtr1-Qtr4); set mylib.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

Rotating a SAS Data Set: Execution

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of mylib.donate

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22 E00224 4 .

.5.223312E00224

ID Qtr3Qtr1 Qtr2 Qtr AmountQtr4D D D DPDV Automatic return. Continue

processing observations from

mylib.donate.

rotate data set

Page 66: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

67

Rotating a SAS Data Set

Partial PROC PRINT Output

proc print data=rotate noobs;run;

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22 E00224 4 . E00367 1 35 E00367 2 48 E00367 3 40 E00367 4 30

Page 67: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

68

Using Multidimensional Arrays

ARRAY array-name {…,rows, cols} $ length elements (initial values);ARRAY array-name {…,rows, cols} $ length elements (initial values);

General form for the multidimensional ARRAY statement:

rowsspecifies the number of array elements in a row arrangement.

cols specifies the number of array elements in a column

arrangement.Example

array ex{3,4} x1-x12;

Page 68: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Using Multidimensional ArraysFor two-dimensional array, the array statement is ARRAY array_name(row, column) <$> < _temporary_ > <length>

<elements> <(initial values>)

Consider the example:Array new(3,4) X1 – X12;The elements of the new(r,c) is defined as below:NOTE: new(2,3) = X7, new(3,2)=X10The element of new(r,c) = Ncol*(r-1) + cEx, for new(2,3), the element = 4*(2-1)+3 = 7 new(3,2): the element = 4*(3-1)+2=10

X1 X2 X3 X4X5 X6 X7 X8

X9 X10 X11 X12Rows

Columns

Page 69: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

Use of nested DO-LOOP to process multi-dimensional array

Ex, the following example computes quarter sales from monthly sales.Data qt_sales (drop = i j); set Mon_sales;Array m_sale{4,3} month1-month12;Array q_sale{4} ;Do i = 1 to 4;

q_sale{i} = 0;Do j = 1 to 3;

q_sale{i} + m_sale{i,j};End;

End; Run;

NOTE: The final value for i = 5 and j = 4 after completion of the data step.

Page 70: Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.

71

The data set mylib.empdata consists of salary of employees for a company for the year 2006. The salary increment was based on the following percentage for the years 2007 to 2012, respectively:2.5%, 3%, 3%, 3.5% 3.5%, 2% annually. Write a program to compute the salary for each employee using array technique, and round the salary of each year to the nearest integer.

Exercises