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

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Programming with Microsoft Visual Basic 2005, Third Edition
An Introduction to Programming with C++ Fifth Edition
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Programming Logic and Design Fourth Edition, Comprehensive
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
Understanding SAS Data Step Processing Alan C. Elliott stattutorials.com.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 14: Generating Data with Do Loops OBJECTIVES Understand iterative DO loops. Construct a DO loop to perform repetitive calculations Use DO loops.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
12- 1 Chapter 12.  Single-Level OCCURS Clauses  Processing Data Stored in Array  Using OCCURS Clause for Table Handling  Use of SEARCH Statement 
1 Chapter 4: Introduction to Lookup Techniques 4.1 Introduction to Lookup Techniques 4.2 In-Memory Lookup Techniques 4.3 Disk Storage Techniques.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 20 Creating Multiple Observations from a Single Record Objectives Create multiple observations from a single record containing repeating blocks.
A First Book of ANSI C Fourth Edition
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 8 Arrays and Strings
Array Processing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 12 (part 2) Table Handling and Search Techniques.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 6: Arrays: Lists and Tables
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Chapter 16: Using Lookup Tables to Match Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
12- 1 Chapter 12.  Single-Level OCCURS Clauses  Processing Data Stored in Array  Using OCCURS Clause for Table Handling  Use of SEARCH Statement 
1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C arrays ❏ To be able to pass arrays and array elements to functions.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Arrays.
BMTRY 789 Lecture 6: Proc Sort, Random Number Generators, and Do Loops Readings – Chapters 5 & 6 Lab Problem - Brain Teaser Homework Due – HW 2 Homework.
Fortran: Control Structures Session Three ICoCSIS.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 8, 13, & 24 By Tasha Chapman, Oregon Health Authority.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 11 Reading SAS Data
EGR 2261 Unit 10 Two-dimensional Arrays
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 4: Using Lookup Tables to Match Data: Arrays
Chapter 7 Arrays.
Chapter 5: Using DATA Step Arrays
Presentation transcript:

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

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.

3 Performing Repetitive Calculations Employees 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 E E

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

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 E E E E E

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.

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.

8 What Is a SAS Array?... D IDQtr4Qtr2Qtr3Qtr1 CONTRIB First element Second element Third element Fourth element Array name

9 What Is a SAS Array? IDQtr4Qtr2Qtr3Qtr1 CONTRIB First element Second element Third element Fourth element Array references CONTRIB{1}CONTRIB{2}CONTRIB{3}CONTRIB{4} Array name

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} ;

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

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.

Creating an One-Dimensional Array An 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;

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.

15 Defining an Array Write an ARRAY statement that defines the four quarterly contribution variables as elements of an array. array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; First element Second element Third element Fourth element IDQtr4Qtr2Qtr3Qtr1 CONTRIB

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; Qtr4Qrtr2ThrdQQ1 First element Second element Third element Fourth element ID CONTRIB2

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;

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 BA - - B All 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.

Some examples of Array Statements Array 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.

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;

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;

22 CONTRIB{i} Processing an Array array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do i=1 to 4; Contrib{i}=Contrib{i}*1.25; end; Qtr4 Qtr2 Qtr3 Qtr1... First element Second element Third element Fourth element

23 CONTRIB{i} Processing an Array array 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... First element Second element Third element Fourth element

24 CONTRIB{i} 4 CONTRIB{4} 3 CONTRIB{3} 2 CONTRIB{2} Processing an Array array 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 First element Second element Third element Fourth element

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;

26 Performing Repetitive Calculations 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; NOTE: the index i will be created as a new variable with the final value = # of dimension + 1. For the above example, the final value of i = 4+1 = 5. Unless this index will be used later, the index should be DROPPED

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;

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;

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;

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;

31 Performing Repetitive Calculations Partial PROC PRINT Output ID Qtr1 Qtr2 Qtr3 Qtr4 E E E E E proc print data=charity noobs; run;

32 Creating Variables with Arrays Calculate 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 E E

33 Creating Variables with Arrays data 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.

34 Creating Variables with Arrays ID Percent1 Percent2 Percent3 Percent4 E % 49% 33%. E % 31% 26% 20% E % 37% 37% E % 20% 32% 31% E % 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.

35 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 E E First difference

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 E E First difference Second difference

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 E E Third difference First difference Second difference

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;

39 Creating Variables with Arrays ID Diff1 Diff2 Diff3 E E E E E proc print data=change noobs; var ID Diff1-Diff3; run; Partial PROC PRINT Output

Assigning initial values in ARRAY statement There 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 ( ); NOTE: Each variable is assigned an initial value in the same order of the sequence of the initial values.

Assigning initial values in an ARRAY without creating new variables in the SAS data set By 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} ( ); 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_ ( );

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.

43 Assigning Initial Values ID Diff1 Diff2 Diff3 Diff4 E E E E E proc print data=compare noobs; var ID Diff1 Diff2 Diff3 Diff4; run; Partial PROC PRINT Output

44... IDQtr3Qtr1Qtr2Qtr4 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 E E Partial Listing of mylib.donate Creating Variables with Arrays: Compilation

45... IDQtr3Qtr1Qtr2Diff1Diff2Qtr4 PDV Diff3Diff4 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 E E Partial Listing of mylib.donate Creating Variables with Arrays: Compilation

46... IDQtr3Qtr1Qtr2Diff1Diff2Qtr4 PDV Diff3Goal2Diff4Goal1Goal4Goal3 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 E E Partial Listing of mylib.donate Creating Variables with Arrays: Compilation

47... IDQtr3Qtr1Qtr2Diff1Diff2Qtr4 PDV Diff3Goal2Diff4Goal1Goal4iGoal3 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 E E Partial Listing of mylib.donate Creating Variables with Arrays: Compilation

48 IDQtr3Qtr1Qtr2Diff1Diff2Qtr4 PDV Diff3Goal2Diff4Goal1Goal4iGoal3 DDDD 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 E E Partial Listing of mylib.donate Creating Variables with Arrays: Compilation

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 variables If we did not use _temporary_, then SAS would have created Goal1 – Goal4 as well.

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} $ ; $ 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.

51 Rotating a SAS Data Set Rotating, 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 E E

52 ID Qtr Amount Partial Listing of rotate ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of mylib.donate E E E E E E E E Rotating a SAS Data Set

53 Rotating a SAS Data Set 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)The variable Qtr is created to be 1,2,3, 4 2)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.

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 Amount rotate data set Rotating a SAS Data Set: Compilation PDV IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD

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 E E Partial Listing of mylib.donate PDV IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD

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; E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD Rotating a SAS Data Set: Execution ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of mylib.donate PDV

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; E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD Rotating a SAS Data Set: Execution ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of mylib.donate PDV

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; E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD Rotating a SAS Data Set: Execution ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of mylib.donate Amount=Contrib{1}; PDV

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 E E Partial Listing of mylib.donate E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD ID Qtr Amount E rotate data set PDV

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; E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD Rotating a SAS Data Set: Execution ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of mylib.donate Amount=Contrib{2}; PDV

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 E E Partial Listing of mylib.donate E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD ID Qtr Amount E E PDV rotate data set

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; E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD Rotating a SAS Data Set: Execution ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of mylib.donate Amount=Contrib{3}; PDV

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 E E Partial Listing of mylib.donate E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD ID Qtr Amount E E E PDV rotate data set

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; E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD Rotating a SAS Data Set: Execution ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of mylib.donate Amount=Contrib{4}; PDV

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 E E Partial Listing of mylib.donate E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD ID Qtr Amount E E E E PDV rotate data set

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 E E Partial Listing of mylib.donate ID Qtr Amount E E E E E00224 IDQtr3Qtr1Qtr2QtrAmountQtr4 DDDD PDV Automatic return. Continue processing observations from mylib.donate. rotate data set

67 Rotating a SAS Data Set Partial PROC PRINT Output proc print data=rotate noobs; run; ID Qtr Amount E E E E E E E E

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: rows specifies 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;

Using Multidimensional Arrays For two-dimensional array, the array statement is ARRAY array_name(row, column) ) 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)=X10 The element of new(r,c) = Ncol*(r-1) + c Ex, for new(2,3), the element = 4*(2-1)+3 = 7 new(3,2): the element = 4*(3-1)+2=10 X1X2X3X4 X5X6X7X8 X9X10X11X12 Rows Columns

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; Run; NOTE: The final value for i = 5 and j = 4 after completion of the data step.

71 The data set mylib.empdata consists of salary of employees for a company for the year 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