SAS Array – A Perfect Guide of SAS Array Operators with Syntax

Expert-led Online Courses: Elevate Your Skills, Get ready for Future - Enroll Now!

After knowing about the SAS String Functions concept, we will be learning about SAS Array. Generally, programmers use SAS arrays to simplify their code which results in less error-prone and more efficient programs.

After completing this tutorial, you will be able to understand:

  • SAS Array Concept
  • SAS Array Syntax
  • SAS Array Declaration
  • SAS Array Operators

What is Array in SAS?

SAS array groups similar variables for processing inside the data step. After SAS array is defined, the tasks performed by variables at different times can be performed using a single array. This saves time and does not require multiple statements to be written.

Consider, SAS Array example, a savings data set (savings) that contains 24 monthly variables for a single year, 12 variables for income(Inc1–Inc12), and 12 variables for expenses (Exp1 – Exp12). To calculate the net savings for each month, the SAS program needs 12 statements:

net_sav1 = inc1- exp1;
net_sav2 = inc2- exp2; . . .
.
.
.
.
net_sav3 = inc3- exp3;
net_sav11 = inc11-exp11;
net_sav12 = inc12- exp12;

This method for calculating the net savings is repetitive. As the amount of data increases, more statements are required to calculate net savings for each month. Arrays helps to perform these calculations with few statements.

  1. In almost all cases, a code that is written with arrays can also be written without using arrays.
  2. Arrays provide an alternative method to refer to a variable rather than using the name of the variable at multiple places and times.

SAS Array Syntax

The SAS ARRAY statement consists of the keyword ARRAY followed by the name of the array:

ARRAY array-name[ ];

The SAS array name can be followed by either a pair of parentheses ( ), braces { }, or square brackets [ ]. By specifying a value inside the bracket, we can assign the same number of variables to the array.

For example – using the same example used above, we can create an array income and add 12 variables to it, one for each month, instead of writing statements for those 12 variables.

ARRAY income[12] inc1-inc12;

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

In this statement, the array income has 12 variables (inc1–inc12) associated with it.

 

SAS Array Income Statement

Examples of SAS Array Declaration

# Declare an array of length 4 named age with values.

ARRAY age[4] (11 1 2 62);

# Declare an array of length 8 named colors with values starting at index 0.

ARRAY colors (0:8) A B C D E F G H I;

# Declare an array of length 5 named books which contain character values.

ARRAY books(1:5) $ b1-b5;

# Declare an array of required length depending on the number of values supplied.

ARRAY ANS(*) A1-A10;

Points to Remember

There are some important points, which you should remember while working on SAS Array. SAS Variables that are associated with an array have certain characteristics:

  1. All variables that are defined inside an array should be of the same type. They can be either numeric or character variables.
  2. If the variables are character variables, a dollar ($) sign must be placed after the defining the array.

Example –

array my_name[3] $ first middle last;

By default, variables inside the array have a length of 8 bytes. To specify a different length for the variables, include the desired length after the $ for character arrays and after the brackets for numeric arrays.

Example –  

array name[3] $12 first last middle;

SAS Array Operators

Here, we talk about two types of Array Operators in SAS: OF Operators and IN Operators.

 SAS Array Operators

1. SAS OF Operator

We use the OF operator when a calculation is to be performed on all the variables or elements of the array.

DATA example_OF;
INPUT A1 A2 A3 A4;
ARRAY A(4) A1-A4;
A_SUM=SUM(OF A(*));
A_MEAN=MEAN(OF A(*));
A_MIN=MIN(OF A(*));
DATALINES;
21 4 52 11
96 25 42 6;
RUN;
PROC PRINT DATA=example_OF;
RUN;

Output:

SAS OF Operator

2. SAS IN Operator

If we want to look for a particular value in the array and check for its presence, we can make use of IN operator. In the below example, we will check for the availability of the color “Pink” in the data. The keyword IN is case sensitive.

DATA in_example;
INPUT A1 $ A2 $ A3 $ A4 $;
ARRAY COLOURS(4) A1-A4;
IF 'yellow' IN COLOURS THEN available='Yes'; ELSE available='No';
DATALINES;
Orange-pink violet yellow;
RUN;
PROC PRINT DATA=in_example;
RUN;

Output:

SAS IN Operator

Summary

In this SAS Array Tutorial, we studied different aspects of SAS Array, how they are important in improving the efficiency of code, reduce redundancy, and how to make our program more easy and organized. So, stay tuned for more updates.

Any queries? Feel free to ask in the comment section. We will be glad to hear from you.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.

Leave a Reply

Your email address will not be published. Required fields are marked *