May 26, 2021 SAS
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.
In SAS, arrays are declared using the following syntax:
ARRAY ARRAY-NAME(SUBSCRIPT) ($) VARIABLE-LIST ARRAY-VALUES
In the syntax above:
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;
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:
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:
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: