Sas 简明教程
SAS - Arrays
SAS 中的数组用于使用索引值存储和检索一系列值。索引表示保留内存区域中的位置。
Arrays in SAS are used to store and retrieve a series of values using an index value. The index represents the location in a reserved memory area.
Syntax
在 SAS 中,使用以下语法声明数组 −
In SAS an array is declared by using the following syntax −
ARRAY ARRAY-NAME(SUBSCRIPT) ($) VARIABLE-LIST ARRAY-VALUES
在以上语法中 −
In the above syntax −
-
ARRAY is the SAS keyword to declare an array.
-
ARRAY-NAME is the name of the array which follows the same rule as variable names.
-
SUBSCRIPT is the number of values the array is going to store.
-
($) is an optional parameter to be used only if the array is going to store character values.
-
VARIABLE-LIST is the optional list of variables which are the place holders for array values.
-
ARRAY-VALUES are the actual values that are stored in the array. They can be declared here or can be read from a file or dataline.
Examples of Array Declaration
可以使用以上语法以多种方式声明数组。以下为示例。
Arrays can be declared in many ways using the above syntax. Below are the examples.
# 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;
Accessing Array Values
可以通过使用 print 过程来访问存储在数组中的值,如下所示。在一个数组值是用上述其中一种方法声明之后,使用 DATALINES 语句提供数据。
The values stored in an array can be accessed by using the print procedure as shown below. After it is declared using one of the above methods, the data is supplied using DATALINES statement.
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 above code, it produces following result −
Using the OF operator
当分析来自数组的数据以对数组的整行执行计算时,OF 运算符将用于。在下面的示例中,我们对每一行的值应用总和和平均值。
The OF operator is used when analysing the data forma an Array to perform calculations on the entire row of an array. In the below example we apply the Sum and Mean of 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 above code, it produces following result −
Using the IN operator
还可以使用 IN 运算符访问数组中的值,该运算符检查数组行中是否存在某个值。在下面的示例中,我们检查数据中是否有颜色 “Yellow” 。此值为区分大小写。
The value in an array can also be accessed using the IN operator which checks for the presence of a value in the row of the array. In the below example we check for the availability of the colour "Yellow" in the data. This 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 above code, it produces following result −