SAS arrays sound fancy. They are not. Think of an array as a neat little basket for variables that already exist in your DATA step. You can loop through the basket, change values, check for problems, or create new variables. It saves typing. It also saves your sanity.
TLDR: A SAS array is a temporary name for a group of variables. It helps you process many variables with one loop instead of many repeated lines. Arrays are used inside the DATA step and disappear after the step finishes. They are great for cleaning data, recoding values, finding missing values, and building new variables.
What Is a SAS Array?
A SAS array is not a new data set. It is not a permanent object. It is a shortcut.
Imagine you have five test scores:
- score1
- score2
- score3
- score4
- score5
You want to add 5 points to each score. Without an array, you write five lines. That is boring. With an array, you write one loop. Much better.
The Basic Syntax
Here is the common pattern:
array array_name {number_of_items} variable_list;
For example:
array scores {5} score1-score5;
This says: “Hey SAS, call these five variables scores for now.”
You can then refer to them like this:
scores{1}meansscore1scores{2}meansscore2scores{5}meansscore5
The number in braces is the position. It is like a seat number at a tiny data theater.
Example 1: Add Points to Test Scores
Let us give every student 5 bonus points. Because we are kind today.
data want;
set have;
array scores {5} score1-score5;
do i = 1 to 5;
scores{i} = scores{i} + 5;
end;
drop i;
run;
What happens here?
- SAS reads one row from
have. - It groups
score1throughscore5into an array. - The loop visits each score.
- It adds 5.
- The variable
iis dropped, because it was only a helper.
This is cleaner than writing:
score1 = score1 + 5;
score2 = score2 + 5;
score3 = score3 + 5;
score4 = score4 + 5;
score5 = score5 + 5;
Arrays help you avoid copy and paste mistakes. Those little gremlins love repeated code.
Example 2: Replace Missing Values
Missing values are common. They sneak into data like socks vanish in laundry.
Suppose you have monthly sales variables:
- jan
- feb
- mar
- apr
- may
- jun
You want missing sales to become 0.
data sales_clean;
set sales_raw;
array months {6} jan feb mar apr may jun;
do i = 1 to 6;
if months{i} = . then months{i} = 0;
end;
drop i;
run;
In SAS, a missing numeric value is written as a period. So this line checks for it:
if months{i} = . then months{i} = 0;
Simple. Fast. Tidy.
Using the Star Shortcut
You do not always need to count variables yourself. SAS can count them for you.
array months {*} jan feb mar apr may jun;
The star means: “SAS, you count them.”
This is useful when the list is long. It also reduces errors. SAS is good at counting. Humans are good at snacks.
Then use dim() in the loop:
data sales_clean;
set sales_raw;
array months {*} jan feb mar apr may jun;
do i = 1 to dim(months);
if months{i} = . then months{i} = 0;
end;
drop i;
run;
dim(months) returns the number of items in the array. If the array has 6 variables, it returns 6.
Example 3: Create New Variables
Arrays can also help create new variables.
Suppose you have raw scores and need pass or fail flags.
data results;
set tests;
array score {*} math science english;
array pass {*} pass_math pass_science pass_english;
do i = 1 to dim(score);
if score{i} >= 70 then pass{i} = 1;
else pass{i} = 0;
end;
drop i;
run;
Here we use two arrays:
scoreholds the original score variables.passholds the new pass flag variables.
The first item in score lines up with the first item in pass. So math matches pass_math. Nice and orderly.
Character Arrays
SAS arrays can hold character variables too. But all variables in one array must be the same type. Do not mix numeric and character variables in the same array. SAS will not enjoy that party.
Here is a character example. We want to replace blank survey answers with Unknown.
data survey_clean;
set survey_raw;
array answers {*} $ q1 q2 q3 q4;
do i = 1 to dim(answers);
if answers{i} = "" then answers{i} = "Unknown";
end;
drop i;
run;
The dollar sign tells SAS this is a character array.
Temporary Arrays
A temporary array stores values during the DATA step. These values are not written to the output data set.
They are handy for lookup lists, weights, or small tables.
data weighted;
set scores;
array score {*} test1 test2 test3;
array weight {3} _temporary_ (0.2 0.3 0.5);
final_score = 0;
do i = 1 to dim(score);
final_score + score{i} * weight{i};
end;
drop i;
run;
Here, the weights are stored in memory. They do not become variables. They just help calculate final_score.
Common SAS Array Rules
Here are the big rules. Tape them to your coding wall.
- Arrays exist only in the DATA step. They are temporary helpers.
- An array is not a data set. It is a way to reference variables.
- One array cannot mix numeric and character variables. Keep types separate.
- Use
dim()when possible. It makes loops safer. - Drop loop counters. Variables like
iare usually not needed later. - Use clear names. Names like
scoresormonthsare friendly.
When Should You Use Arrays?
Use arrays when you need to do the same thing to many variables.
Good uses include:
- Changing missing values to 0
- Recoding survey answers
- Creating flags
- Adding or subtracting values
- Calculating weighted scores
- Checking many variables for errors
Do not use arrays just to look clever. Use them when they make code shorter and clearer.
A Tiny Debugging Tip
If your array is not working, check three things first:
- Are all variables the same type?
- Does the number of variables match the array size?
- Did you spell every variable name correctly?
Also, remember this little friend:
put i= scores{i}=;
This can print values to the log while the loop runs. It is like turning on a flashlight inside your DATA step.
Final Thoughts
SAS arrays are simple once you stop treating them like mystery machines. They are just temporary labels for groups of variables. Add a loop, and they become very powerful.
They help you write less code. They reduce errors. They make repetitive work feel less like chewing cardboard.
Start small. Try cleaning missing values. Then try creating flags. Soon, arrays will feel like one of the friendliest tools in your SAS toolbox.

