Professional Documents
Culture Documents
SAS Advance-3-1
SAS Advance-3-1
1
April 2009
Topics covered
SAS Arrays
SAS
Sample Code
INTRODUCTION
DEFINITION OF AN ARRAY
Syntax:ARRAY arrayname{n} <$><length> array elements<(initial
value-final value)>;
Where ;
Arrayname is a valid SAS variable name
n : is number of elements in the array
$ : attribute of the array element (character in this case)
Length : common length of array elements in general.
4
CONTINUED
Array elements are the SAS variables which are defined
together in the Array.
Initial and final values are the boundary variables.
E.g.
ARRAY actcode{10}
ARRAY months{3}
$ actcode1 actcode10;
$ SEP OCT NOV;
CONTINUED:
The array elements should be of the same data type, i.e.
either all numeric or all character.
The array elements needn't be related to each other.
Each element of an array can be of varying length but it is
necessary to specify the maximum length that an element
can have.
The subscript n can be either of the following: A numeric constant
A variable whose value is numeric
A numeric SAS expression.
The asterisk (*), which is used when the number of
elements in the array cannot be predetermined.
CONTINUED:
The subscript must necessarily be enclosed in either of the
three brackets:- Braces{}, square[], or parenthesis().
The array elements cannot be used as variables in
statements like DROP and KEEP, which are compiler
statements, since ARRAY statement itself is a compiler
statement.
There are special variables, that can be used to select all
variables, or all variables of the same data type.
CONTINUED:
_NUMERIC_ : signifies that all numbers are elements of
the array.
_CHARACTER_ :- signifies that all character variables
are elements of the array.
_ALL_ :- signifies that all variables are array elements.
E.g.
REFERENCING AN ARRAY:
The ARRAY statement that defines the array in a DATA step
should be present before the array is referenced in the same
DATA step .
An array element is referenced by using the arrayname along
with the subscript n, which would identify the particular
element of the array, as it specifies the position of the element
within the array.
Syntax:-
array name(n)
10
CONTINUED:
E.g. :ARRAY Months{3} 3
Array Element
Jan
Array Reference
Months{1}
Feb
Months{2}
Mar
Months{3}
11
CONTINUED:
12
EXAMPLE:
DATA TEMP1;
SET TEMP2;
/* defining the array before it is referenced */
ARRAY BIC{10} $ BIC1 BIC10;
ARRAY LOB{10} $ LOB1 LOB10;
/*referencing the array */
DO N = 1 to 10;
BIC{n} = PUT(LOB{n},$LOBFMT.);
END;
This loop will go on for 10 iterations.
13
THANK YOU
For any queries drop a mail to
HJW_SOS-SAS@infosys.com
14