SAS-Array
On Operator | In Operator
An array is a data structure which is used to store a fixed-size collection of elements. An array can store only the elements of the same data type.
It can store a collection of data but often used to store a collection of variables of the same data type.
The Array uses the index to store the elements. It reduces the number of lines of the code.
In SAS, the Array does the same task as described above. It means that it is used to store and retrieve a series of values by using the index value. The index represents the location of the element in a reserved memory area.
Syntax:
Where,
- ARRAY: Itis the keyword to declare an Array.
- ARRAY_NAME: It is the name of the Array. It follows the name conventions same as a variable.
- SUBSCRIPT: It is the number of values which we store in the Array.
- ($): It is used only when the Array stores the character values.
- VARIABLE-LIST: It is an optional list of variables, and these variables are the place holders for Array values.
- ARRAY-VALUES: These are the actual values that will be stored in the Array. We can either declare these values or read from a file or datalines.
Array Declaration
We can declare Array in several ways by using the above syntax. Following are the examples for array declaration.
- The name NUMBER declares an Array. Its length is 5 with the values 42, 88, 67, 62, 44.
- The name ALPHABET declares an Array. Its length is 8 with the values A, B, C, D, E, F, G, H, and it is starting at index 0.
- The name SEARCH declares an Array of length 5, and the contained values are characters.
- The name FEEDBACK declares an Array. In this Array, we can take the lengthas per our requirement.
Accessing Array Values
After being declared, we can supply data values in an Array by using the DATALINES statement, and we can access these stored values by using the PRINT procedure.
In the following example, we are going to store and accessthe values of an Array.
Execute the above code in SAS studio:
Output:
As we can see in the output, all values of the Array have been stored and accessed.
Concatenate multiple values of an Array
We can concatenate multiple values of Array by declaring a new variable.
Example:
Output:
As we can see in the output, both values, i.e., a1 and a2, have been concatenated into the variable mix.
OF Operator
The OF Operator is used when the data format of an Array is analyzed to calculate the entire row of an Array. In the following example, we are going to calculate the Sum and Mean of values in each row.
Execute the above code in SAS studio:
Output:
As we can see in the output, entire rows have been calculated.
IN Operator
In an Array, we can access the particular value by using the IN operator. IN Operator checks the status of presence or absence of specific value in the row of the Array. In the following example, we are going to check for the availability of the animal “Tiger” in the dataset. This value is case sensitive.
Execute the above code in SAS studio:
Output:
As we can see in the output, the Lion is present in the data set,that’s why it is showing yes in the “available” column.