Sas 简明教程
SAS - Variables
通常,SAS 中的变量表示其正在分析的数据表的列名。但它也可以用于其他目的,如在编程循环中将它用作计数器。在当前章节中,我们将看到 SAS 变量用作 SAS 数据集的列名的用法。
In general variables in SAS represent the column names of the data tables it is analysing. But it can also be used for other purpose like using it as a counter in a programming loop. In the current chapter we will see the use of SAS variables as column names of SAS Data Set.
SAS Variable Types
SAS 具有以下三种类型的变量:
SAS has three types of variables as below −
Numeric Variables
这是默认的变量类型。这些变量用于数学表达式中。
This is the default variable type. These variables are used in mathematical expressions.
Syntax
INPUT VAR1 VAR2 VAR3; #Define numeric variables in the data set.
在以上语法中,INPUT 语句显示了数字变量的声明。
In the above syntax, the INPUT statement shows the declaration of numeric variables.
Character Variables
字符变量用于数学表达式中不使用的值。它们被视为文本或字符串。通过在变量名称末尾添加带空格的 $ 符号,变量变为字符变量。
Character variables are used for values that are not used in Mathematical expressions. They are treated as text or strings. A variable becomes a character variable by adding a $ sing with a space at the end of the variable name.
Syntax
INPUT VAR1 $ VAR2 $ VAR3 $; #Define character variables in the data set.
在以上语法中,INPUT 语句显示了字符变量的声明。
In the above syntax, the INPUT statement shows the declaration of character variables.
Date Variables
这些变量只被视为日期且需要为有效的日期格式。通过在变量名称末尾添加带空格的日期格式,变量变为日期变量。
These variables are treated only as dates and they need to be in valid date formats. A variable becomes a date variable by adding a date format with a space at the end of the variable name.
Use of Variables in SAS Program
如以下示例中所示,上述变量用于 SAS 程序中。
The above variables are used in SAS program as shown in below examples.
Example
以下代码显示了如何声明并在 SAS 程序中使用这三种类型的变量
The below code shows how the three types of variables are declared and used in a SAS Program
DATA TEMP;
INPUT ID NAME $ SALARY DEPT $ DOJ DATE9. ;
FORMAT DOJ DATE9. ;
DATALINES;
1 Rick 623.3 IT 02APR2001
2 Dan 515.2 OPS 11JUL2012
3 Michelle 611 IT 21OCT2000
4 Ryan 729 HR 30JUL2012
5 Gary 843.25 FIN 06AUG2000
6 Tusar 578 IT 01MAR2009
7 Pranab 632.8 OPS 16AUG1998
8 Rasmi 722.5 FIN 13SEP2014
;
PROC PRINT DATA = TEMP;
RUN;
在以上示例中,所有字符变量在后面声明并加有 $ 符号,所有日期变量在后面声明并加有日期格式。以上程序的输出如下。
In the above example all the character variables are declared followed by a $ sign and the date variables are declared followed by a date format. The output of the above program is as below.
Using the Variables
变量在分析数据中非常有用。它们用于对统计分析应用的表达式中。我们来看一个示例,分析名为 CARS 的内置数据集,该数据集存在于 Libraries → My Libraries → SASHELP 下。双击它以浏览变量及其数据类型。
The variables are very useful in analysing the data. They are used in expressions in which the statistical analysis is applied. Let’s see an example of analysing the built-in Data Set named CARS which is present under Libraries → My Libraries → SASHELP. Double click on it to explore the variables and their data types.
接下来,我们可以使用 SAS studio 中的任务选项来生成其中一些变量的汇总统计信息。转到 Tasks → Statistics → Summary Statistics 并双击它以打开窗口,如下所示。选择数据集 SASHELP.CARS 并选择分析变量下的三个变量 - MPG_CITY、MPG_Highway 和 Weight。在单击选择变量时按住 Ctrl 键。单击运行。
Next we can produce a summary statistics of some of these variables using the Tasks options in SAS studio. Go to Tasks → Statistics → Summary Statistics and double click it to open the window as shown below. Choose Data Set SASHELP.CARS and select the three variables - MPG_CITY, MPG_Highway and Weight under the Analysis Variables. Hold the Ctrl key while selecting the variables by clicking. Click run.
在执行以上步骤后单击结果选项卡。它显示了所选三个变量的统计汇总。最后一列表明用于分析的观测值(记录)数量。
Click on the results tab after the above steps. It shows the statistical summary of the three variables chosen. The last column indicates number of observations (records) used in the analysis.