Wednesday, October 30, 2013

ARRAY

What is Array?


Definition: It is grouping of variables that exists only for duration of DATA step. Its dimension is specified by number in curly braces. Arrays get their power from the ability to reference elements by an index value.

How to assign initial values to array elements?

Initial values can be assigned by enclosing values in parentheses. 
        array firstq{3} m1 m2 m3 (80 85 82);
Essentially element m1 is set to 80.

What are types of arrays?

There are two types: numeric and character.
Once array is created it is designed to contain only one type of data, either numeric or character.
Numeric:
      array combination{3} color1-color3 (255 100 202);
Character:
      array combination{3} $ 16 color1 color2 color3 ('red' 'green' 'blue');

What does the number 16 signify?
It determines length of character array element. It overrides default value of 8 for character array and sets it to 16.

How to name array elements?

Array elements can be specified as variable list or range.
Example of variable list for academic calendar:
        array acadcal{12} m1-m12;
Example of range:
        array lastq{10:12} m10 m11 m12;

Is there a function to determine length of array?

Of course! It is possible to create an array without specifying how many elements it will contain and determine those number of elements, rather dimension, later by means of function DIM.

   data work.college;
     set department.class;  
     retain totalenroll;
     totalenroll=0;
     array classsize{*} c1-c100;
     do i=1 to dim(classsize);
        totalenroll=totalenroll+classsize{i};
     end;
   run;

Above snippet calculates total enrollment for all classes offered where each array element stores number of students in a class. Well, what happens to variable i? Will it be written to output data set? That's interesting question and it is answered at the end of this post***.

Can temporary array elements be created?


Yes! with the use of _temporary_ keyword. These array elements are created and used in compilation and execution phase and do not get written to output data set.
        array secondq{3} _temporary_ m4-m5;

How are arrays used?

  • To perform repetitive calculations
  • Create many variables that have same attributes
  • Rotate data sets by changing variables to observations and vice versa
  • Compare variables
  • Perform table lookup
Tips:
  • Do not give an array the same name as a variable in the same DATA step.
  • Initial values can be separated by comma as well.
  • Avoid using name of SAS function
  • Do not use array names in LABEL, FORMAT, DROP, KEEP, LENGTH statements.
  • Dimension of array can be determined by *
  • Parentheses, braces or brackets are good to specify dimension
           array courses(3) art science commerce;
           array courses{3} art science commerce;
           array courses[3] art science commerce;

*** YES, i is written to output data set. It can be eliminated by simple use of drop= option in DATA statement.

No comments:

Post a Comment