Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SAS array


May 26, 2021 SAS


Table of contents


Arrays in SAS are used to store and retrieve a series of values using index values. T he index represents the location in the reserved store.

Syntactic

In SAS, arrays are declared using the following syntax:

ARRAY ARRAY-NAME(SUBSCRIPT) ($) VARIABLE-LIST ARRAY-VALUES

In the syntax above:

  • ARRAY is the SAS keyword that declares an array.
  • ARRAY-NAME is the name of an array that follows the same rules as the variable name.
  • SUBSCRIPT is the number of values that an array wants to store.
  • ($) is an optional parameter that is used only when the array wants to store character values.
  • VARIABLE-LIST is an optional list of variables, which are placeholders for array values.
  • ARRAY-VALUES is the actual value stored in the array. T hey can be declared here, or they can be read from a file or dataline.

An example of an array declaration

Arrays can be declared in a variety of ways using the above syntax. H ere's an example.

# Declare an array of length 5 named AGE with values.
ARRAY AGE[5] (12 18 5 62 44);

# Declare an array of length 5 named COUNTRIES with values starting at index 0.
ARRAY COUNTRIES(0:8) A B C D E F G H I;

# Declare an array of length 5 named QUESTS which contain character values.
ARRAY QUESTS(1:5) $ Q1-Q5;

# Declare an array of required length as per the number of values supplied.
ARRAY ANSWER(*) A1-A100;

Access array values

The values stored in the array can be accessed by using the printing process shown below. A fter declaring using one of the above methods, datalineS statements are used to provide data.

DATA array_example;
INPUT a1 $ a2 $ a3 $ a4 $ a5 $;
ARRAY colours(5) $ a1-a5;
mix = a1||'+'||a2;
DATALINES;
yello pink orange green blue
;
RUN;
PROC PRINT DATA=array_example;
RUN;

When we execute the code above, it produces the following results:

SAS array

Use the OF operator

The OF operator is used when analyzing arrays in the form of arrays to perform calculations on the entire row of the array. I n the following example, we apply the and equal values of the values in each row.

DATA array_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=array_example_OF;
	RUN;

When we execute the code above, it produces the following results:

SAS array

Use the IN operator

You can also use the IN operator to access the values in the array, which checks for the presence of values in the array row. I n the following example, we examine the availability of the color "yellow" in the data. T his value is case sensitive.

DATA array_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=array_in_example;
	RUN;

When we execute the code above, it produces the following results:

SAS array