SAS Arrays
Introduction to Arrays in SAS
In SAS programming, there are many situations where the same operation
must be performed repeatedly on multiple variables. Writing separate
statements for each variable can make the program lengthy, repetitive, and
difficult to maintain.
To solve this problem, SAS provides a powerful feature called an ARRAY.
An ARRAY allows the programmer to group several variables together under
one common name and process them efficiently using loops.
Arrays are one of the most important concepts in SAS DATA step
programming because they:
reduce repetitive coding
improve program readability
simplify data manipulation
improve maintenance
increase processing efficiency
Arrays are heavily used in:
Data cleaning
Data transformation
Statistical preprocessing
Bulk calculations
Data standardization
Missing value handling
Variable conversion
Dataset restructuring
Rotating datasets
Transposing data
Definition of ARRAY
An ARRAY is:
A grouping of several variables under a single name.
The variables grouped inside the array are called ARRAY ELEMENTS.
The array itself acts like a temporary reference structure that helps SAS
process multiple variables efficiently.
Real-Time Analogy of Arrays
Imagine a classroom with multiple students:
Student1
Student2
Student3
Student4
Instead of calling each student individually every time, the teacher creates a
group called:
“Students”
Now the teacher can process all students together.
Similarly in SAS:
Instead of writing:
weight=weight*100;
height=height*100;
age=age*100;
we can group the variables into an array:
array one(3) weight height age;
and process them using a loop.
Uses of Arrays in SAS
Arrays are extremely useful in SAS programming.
1. Perform Same Processing on Multiple Variables
Arrays allow one piece of logic to be applied to many variables.
Example:
score(i)=score(i)+10;
2. Create New Variables Efficiently
Arrays can automatically generate multiple variables with similar attributes.
Example:
array newvars(5) a1-a5;
3. Handle Missing Values
Arrays help replace missing values in many variables simultaneously.
Example:
if marks(i)=. then marks(i)=0;
4. Standardize Variables
Useful in preprocessing and scaling.
Example:
score(i)=score(i)/100;
5. Perform Bulk Calculations
Arrays simplify repetitive calculations.
6. Rotate or Restructure SAS Datasets
Arrays help transform wide-format data into long-format data and vice versa.
7. Reduce Program Length
Without arrays, programs become lengthy and difficult to maintain.
ARRAY Statement
Arrays are created using the ARRAY statement.
General Syntax
ARRAY array-name(dimension) elements;
Components of ARRAY Statement
Component Description
ARRAY Keyword used to define array
array-name Name assigned to the array
dimension Number of variables/elements
elements Variables grouped inside array
Example of ARRAY Statement
array marks(3) math physics chemistry;
Explanation
Part Meaning
marks Array name
3 Number of variables
math physics chemistry Variables inside array
Important Characteristics of Arrays
1. Arrays Exist Only During DATA Step Execution
Arrays are temporary processing structures.
They are not permanent datasets.
2. Array Name Is NOT Stored in Dataset
Only the actual variables are stored.
Example:
array one(3) weight height age;
The name ONE does not appear in output dataset.
3. Array Elements Are Real Variables
The variables inside the array are actual dataset variables.
4. Arrays Work Best with DO Loops
Arrays are usually processed using iterative loops.
Example 1 – Modifying Existing Variables
Program
DATA one(drop=i);
SET [Link];
ARRAY one(3) weight height age;
DO i=1 TO 3;
one(i)=one(i)*100;
END;
RUN;
PROC PRINT;
RUN;
Objective of the Program
This program multiplies the following variables by 100:
weight
height
age
Step-by-Step Explanation
DATA Statement
DATA one(drop=i);
Creates a new dataset named ONE.
The variable i is dropped from final output because it is only a looping
variable.
SET Statement
SET [Link];
Reads observations from the built-in dataset [Link].
ARRAY Declaration
ARRAY one(3) weight height age;
Creates an array named ONE.
Internal Mapping
Array Position Variable
one(1) weight
one(2) height
one(3) age
DO Loop
DO i=1 TO 3;
The loop executes 3 times.
Iteration Details
Iteration Value of i
1 1
2 2
3 3
Processing Statement
one(i)=one(i)*100;
This multiplies each variable by 100.
Internal Execution
First Iteration
weight=weight*100;
Second Iteration
height=height*100;
Third Iteration
age=age*100;
Final Result
All three variables are updated directly.
Important Learning Point
Arrays eliminate repetitive coding.
Instead of writing:
weight=weight*100;
height=height*100;
age=age*100;
we write only one statement.
Example 2 – Creating New Variables Using
Arrays
Program
DATA one(drop=i);
SET [Link];
ARRAY one(3) weight height age;
ARRAY two(3) nwt nht nage;
DO i=1 TO 3;
two(i)=one(i)*100;
END;
RUN;
PROC PRINT;
RUN;
Objective
Create new transformed variables:
nwt
nht
nage
without changing original variables.
First Array
ARRAY one(3) weight height age;
Contains original variables.
Second Array
ARRAY two(3) nwt nht nage;
Creates NEW variables automatically.
Important Observation
If variables do not already exist, SAS automatically creates them.
Internal Mapping
Position Original Variable New Variable
1 weight nwt
2 height nht
3 age nage
Processing Logic
two(i)=one(i)*100;
Internal Execution
First Iteration
nwt=weight*100;
Second Iteration
nht=height*100;
Third Iteration
nage=age*100;
Final Output
Original variables remain unchanged.
New calculated variables are added.
Advantages of This Approach
Preserves original data
Creates transformed variables
Simplifies bulk calculations
Reduces repetitive coding
Example 3 – Temporary Arrays
Program
DATA one(drop=i);
SET [Link];
ARRAY one(3) weight height age;
ARRAY two(3) nwt nht nage;
ARRAY three(3) _temporary_ (100 500 1000);
DO i=1 TO 3;
two(i)=one(i)*three(i);
END;
RUN;
PROC PRINT;
RUN;
What Is a Temporary Array?
A temporary array:
exists only during DATA step execution
is not written to output dataset
stores temporary values in memory
improves performance
Temporary Array Syntax
ARRAY three(3) _temporary_ (100 500 1000);
Explanation
Position Value
three(1) 100
three(2) 500
three(3) 1000
Processing Logic
two(i)=one(i)*three(i);
Internal Execution
First Iteration
nwt=weight*100;
Second Iteration
nht=height*500;
Third Iteration
nage=age*1000;
Important Observation
The array THREE does not appear in the output dataset because it is
temporary.
Advantages of Temporary Arrays
1. Faster Processing
Temporary arrays use memory efficiently.
2. Reduced Dataset Size
Temporary values are not stored.
3. Useful for Lookup Values
Can store constants and reference values.
4. Useful for Calculations
Excellent for multipliers and conversion factors.
Array Referencing
Arrays are accessed using indexes.
Syntax
array-name(index)
Example:
one(2)
refers to:
height
Array Indexing
By default, SAS arrays start from 1.
Index Variable
1 First variable
2 Second variable
3 Third variable
Arrays and DO Loops
Arrays are most commonly used with DO loops.
Why Use Loops?
Loops automatically process all array elements.
Without loops, arrays lose much of their advantage.
Example
DO i=1 TO 3;
Processes all three variables.
Real-Time Applications of Arrays
1. Missing Value Replacement
IF score(i)=. THEN score(i)=0;
2. Standardization
score(i)=score(i)/100;
3. Percentage Conversion
marks(i)=marks(i)*100;
4. Tax Calculations
salary(i)=salary(i)*1.18;
5. Data Cleaning
IF vars(i)<0 THEN vars(i)=.;
6. Character Conversion
Arrays can process multiple character variables.
Difference Between Normal Arrays and
Temporary Arrays
Feature Normal Array Temporary Array
Stored in dataset Yes No
Creates variables Yes No
Stored permanently Yes No
Uses memory only No Yes
Faster processing Moderate High
Best use Dataset variables Lookup/calculation
values
Common Errors in Arrays
1. Dimension Mismatch
Example:
array x(3) a b;
Error because only 2 variables are provided.
2. Invalid Index
Example:
x(5)
Error if array size is only 3.
3. Forgetting DROP Statement
Loop variable i may unnecessarily appear in output dataset.
Best Practices for Arrays
Always use meaningful array names
Use DROP statement for loop variables
Use temporary arrays for constants
Keep dimensions accurate
Combine arrays with loops
Add comments for readability
Important Interview Questions
Q1. What is an ARRAY in SAS?
An ARRAY is a grouping of variables under a single name for efficient
processing.
Q2. Is the array name stored in the dataset?
No.
Only array elements are stored.
Q3. What is TEMPORARY in arrays?
It creates temporary memory locations not written to output dataset.
Q4. Why are arrays used with DO loops?
To efficiently process multiple variables repeatedly.
Q5. Can arrays create new variables?
Yes.
Example:
array vars(3) a b c;
If variables do not exist, SAS creates them automatically.
Summary
Key Points
Arrays group multiple variables under one name
Arrays simplify repetitive processing
Arrays work efficiently with loops
Arrays reduce coding effort
Arrays improve readability
Temporary arrays are memory-only structures
Arrays are heavily used in real-world SAS programming
Core Syntax Recap
ARRAY array-name(dimension) variables;
Examples Covered
Example Purpose
Example 1 Modify existing variables
Example 2 Create new variables
Example 3 Use temporary arrays
Final Conclusion
Arrays are one of the most powerful DATA step features in SAS.
They help programmers write efficient, scalable, readable, and maintainable
code.
Mastering arrays is essential for:
Data manipulation
Statistical programming
Clinical SAS
Data cleaning
ETL processing
Reporting
Advanced SAS programming
A strong understanding of arrays significantly improves SAS programming
efficiency and productivity.