Sas 简明教程
SAS - Input Methods
输入方法用来读取原始数据。原始数据可能是来自外部源或来自流式数据线。输入语句用您分配给每个字段的名称创建一个变量。所以您必须在输入语句中创建一个变量。同一个变量将显示在 SAS 数据集的输出中。下面是 SAS 中提供给不同输入方法。
The input methods are used to read the raw data. The raw data may be from an external source or from in stream datalines. The input statement creates a variable with the name that you assign to each field. So you have to create a variable in the Input Statement. The same variable will be shown in the output of SAS Dataset. Below are different input methods available in SAS.
-
List Input Method
-
Named Input Method
-
Column Input Method
-
Formatted Input Method
下面介绍了每种输入方法的详细信息。
The details of each input method is described as below.
List Input Method
在这种方法中,变量被列出数据类型。原始数据被仔细分析,以便匹配声明变量的顺序和数据。分隔符(通常为空格)在任何相邻列的之间应该统一。任何缺失数据都会导致输出结果错误,从而产生问题。
In this method the variables are listed with the data types. The raw data is carefully analysed so that the order of the variables declared matches the data. The delimiter (usually space) should be uniform between any pair of adjacent columns. Any missing data will cause problem in the output as the result will be wrong.
Example
以下代码和输出演示了列表输入方法的使用。
The following code and the output shows the use of list input method.
DATA TEMP;
INPUT EMPID ENAME $ DEPT $ ;
DATALINES;
1 Rick IT
2 Dan OPS
3 Tusar IT
4 Pranab OPS
5 Rasmi FIN
;
PROC PRINT DATA = TEMP;
RUN;
运行上述代码时,我们会得到以下输出。
On running the bove code we get the following output.
Named Input Method
在这种方法中,变量被列出数据类型。原始数据被修改,以便匹配数据之前声明的变量名。分隔符(通常为空格)在任何相邻列的之间应该统一。
In this method the variables are listed with the data types. The raw data is modified to have variable names declared in front of the matching data. The delimiter (usually space) should be uniform between any pair of adjacent columns.
Example
以下代码和输出演示了命名输入方法的使用。
The following code and the output show the use of Named Input Method.
DATA TEMP;
INPUT
EMPID= ENAME= $ DEPT= $ ;
DATALINES;
EMPID = 1 ENAME = Rick DEPT = IT
EMPID = 2 ENAME = Dan DEPT = OPS
EMPID = 3 ENAME = Tusar DEPT = IT
EMPID = 4 ENAME = Pranab DEPT = OPS
EMPID = 5 ENAME = Rasmi DEPT = FIN
;
PROC PRINT DATA = TEMP;
RUN;
运行上述代码时,我们会得到以下输出。
On running the bove code we get the following output.
Column Input Method
此方法中,将变量与数据类型和列的宽度放在一起,其中宽度指定数据单列的值。例如,如果员工姓名最多包含 9 个字符,并且每个员工姓名都从第 10 列开始,则员工姓名变量的列宽为 10-19。
In this method the variables are listed with the data types and width of the columns which specify the value of the single column of data. For example if an employee name contains maximum 9 characters and each employee name starts at 10th column, then the column width for employee name variable will be 10-19.
Example
下面的代码显示了列输入方法的用法。
Following code shows the use of Column Input Method.
DATA TEMP;
INPUT EMPID 1-3 ENAME $ 4-12 DEPT $ 13-16;
DATALINES;
14 Rick IT
241Dan OPS
30 Sanvi IT
410Chanchal OPS
52 Piyu FIN
;
PROC PRINT DATA = TEMP;
RUN;
当我们执行以上代码时,会产生以下结果 -
When we execute above code, it produces following result −
Formatted Input Method
此方法从固定起始点读取变量,直到遇到空格。因为每个变量都有固定的起始点,任何一对变量之间的列数便成为第一个变量的宽度。字符“@n”用于将变量的起始列位置指定为第 n 列。
In this method the variables are read from a fixed starting point until a space is encountered. As every variable has a fixed starting point, the number of columns between any pair of variables becomes the width of the first variable. The character '@n' is used to specify the starting column position of a variable as the nth column.
Example
下面的代码显示了格式化输入方法的用法
The following code shows the use of Formatted Input Method
DATA TEMP;
INPUT @1 EMPID $ @4 ENAME $ @13 DEPT $ ;
DATALINES;
14 Rick IT
241 Dan OPS
30 Sanvi IT
410 Chanchal OPS
52 Piyu FIN
;
PROC PRINT DATA = TEMP;
RUN;
当我们执行以上代码时,会产生以下结果 -
When we execute above code, it produces following result −