Sas 简明教程
SAS - Cross Tabulations
交叉制表包括使用两个或更多变量的所有可能的组合来生成交叉表,也称为列联表。在 SAS 中,它是使用 PROC FREQ 与 TABLES 选项一起创建的。例如 - 如果我们需要每个车型类别中每个品牌的每种车型的频率,那么我们需要使用 PROC FREQ 的 TABLES 选项。
Cross tabulation involves producing cross tables also called contingent tables using all possible combinations of two or more variables. In SAS it is created using PROC FREQ along with the TABLES option. For example - if we need the frequency of each model for each make in each car type category, then we need to use the TABLES option of PROC FREQ.
Syntax
在 SAS 中应用交叉制表的语法基本语法为:
The basic syntax for applying cross tabulation in SAS is −
PROC FREQ DATA = dataset;
TABLES variable_1*Variable_2;
以下是所用参数的描述 -
Following is the description of the parameters used −
-
Dataset is the name of the dataset.
-
Variable_1 and Variable_2 are the variable names of the dataset whose frequency distribution needs to be calculated.
Example
考虑查找在从 SASHELP.CARS 创建的数据集汽车 1 中,每个汽车品牌下有多少车型,如下所示。在这种情况下,我们需要各个频率值以及跨品牌和跨车型的频率值总数。我们可以观察到结果跨行和列显示值。
Consider the case of finding how many car types are available under each car brand from the dataset cars1 which is created form SASHELP.CARS as shown below. In this case we need the individual frequency values as well as the sum of the frequency values across the makes and across the types. We can observer that the result shows values across the rows and the columns.
PROC SQL;
create table CARS1 as
SELECT make, type, invoice, horsepower, length, weight
FROM
SASHELP.CARS
WHERE make in ('Audi','BMW')
;
RUN;
proc FREQ data = CARS1;
tables make*type;
run;
在执行以上代码后,我们将得到以下结果:
When the above code is executed, we get the following result −
Cross tabulation of 3 Variables
当我们有三个变量时,我们可以将其中的 2 个分组,并用第三个变量交叉制表它们。因此,结果中有两个交叉表。
When we have three variables we can group 2 of them and cross tabulate each of these two with the third varaible. So in the result we have two cross tables.
Example
在下面的示例中,我们找到了每种汽车品牌对于每种汽车类型和每种汽车型号的频率。我们还使用 nocol 和 norow 选项以避免总和和百分比值。
In the below example we find the frequency of each type of car and each model of car with respect to the make of the car. Also we use the nocol and norow option to avoid the sum and percentage values.
proc FREQ data = CARS2 ;
tables make * (type model) / nocol norow nopercent;
run;
在执行以上代码后,我们将得到以下结果:
When the above code is executed, we get the following result −
Cross tabulation of 4 Variables
使用 4 个变量时,成对组合的数量增加至 4。组 1 中的每个变量都与组 2 的每个变量配对。
With 4 variables, the number of paired combinations increases to 4. Each variable from group 1 is paired with each variable of group 2.
Example
在下面的示例中,我们找到了对于每个品牌和每个型号的汽车的长度频率。同样,对于每个品牌和每个型号的马力的频率。
In the below example we find the frequency of length of the car for each make and each model. Similarly the frequency of horsepower for each make and each model.
proc FREQ data = CARS2 ;
tables (make model) * (length horsepower) / nocol norow nopercent;
run;
在执行以上代码后,我们将得到以下结果:
When the above code is executed, we get the following result −