Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

SAS Advance-3.

1
April 2009

Infosys Technologies Ltd,


Pune

Agenda for Day 5

Topics covered
SAS Arrays
SAS

Sample Code

INTRODUCTION

Array is a temporary grouping of SAS variables (either all


numeric or all characters)
An array must be defined in each data step in which it has to
be referenced.
It exists only for the data step in which it is defined.

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;

POINTS TO REMEMBER WHILE DEFINING:


An array must be defined in each data step in which it
has to be referenced. An array definition does not carry
across independent datasets.
Arrayname follows all the SAS variable naming
rules, i.e. it can be any valid SAS name. But the
arrayname should not be one of the variable names
already present in the dataset in which the array is
being defined.

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.

ARRAY money{*} _NUMERIC_;


ARRAY name{*} _CHARACTER_;
ARRAY A{*}
_ALL_;

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 Feb Mar ;

Jan

Array Reference
Months{1}

Feb

Months{2}

Mar

Months{3}

11

CONTINUED:

Arrays can be referenced using DO groups.


To use arrays with an iterative DO loop:The from and to values of the DO statement should
correspond with the starting and ending elements of the array
which is to be processed.
The index variable of the iterative DO statement should be
used as the array subscript while referencing.

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

You might also like