Sas 简明教程

SAS - Arithmetic Mean

算术平均值是通过对数字变量的值求和,然后将总和除以变量个数获得的值。它也称为平均值。在 SAS 中,算术平均值使用 PROC MEANS 计算。使用此 SAS 过程,我们可以找到所有变量或数据集的某些变量的平均值。我们还可以形成组并找到特定于该组的值的变量的平均值。

The arithmetic mean is the value obtained by summing value of numeric variables and then dividing the sum with the number of variables. It is also called Average. In SAS arithmetic mean is calculated using PROC MEANS. Using this SAS procedure we can find the mean of all variables or some variables of a dataset. We can also form groups and find mean of variables of values specific to that group.

Syntax

在 SAS 中计算算术平均值的基本语法是 −

The basic syntax for calculating arithmetic mean in SAS is −

PROC MEANS DATA = DATASET;
CLASS Variables ;
VAR Variables;

以下是所用参数的描述 −

Following is the description of parameters used −

  1. DATASET − is the name of the dataset used.

  2. Variables − are the name of the variable from the dataset.

Mean of a Dataset

数据集中的每个数字变量的平均值是通过使用 PROC 计算的,仅提供数据集名称而不提供任何变量。

The mean of each of the numeric variable in a dataset is calculated by using the PROC by supplying only the dataset name without any variables.

Example

在下面的示例中,我们找到名为 CARS 的 SAS 数据集中所有数字变量的平均值。我们将小数点后最多位数指定为 2,并找到这些变量的总和。

In the below example we find the mean of all the numeric variables in the SAS dataset named CARS. We specify the maximum digits after decimal place to be 2 and also find the sum of those variables.

PROC MEANS DATA = sashelp.CARS Mean SUM MAXDEC=2;
RUN;

执行以上代码后,我们将得到以下输出 −

When the above code is executed, we get the following output −

Mean

Mean of Select Variables

我们可以通过在 var 选项中提供变量的名称来获得某些变量的平均值。

We can get the mean of some of the variables by supplying their names in the var option.

Example

在下文中,我们计算三个变量的平均值。

In the below we calculate the mean of three variables.

PROC MEANS DATA = sashelp.CARS mean SUM MAXDEC=2 ;
var horsepower invoice EngineSize;
RUN;

执行以上代码后,我们将得到以下输出 −

When the above code is executed, we get the following output −

Mean select variables

Mean by Class

我们可以通过使用其他一些变量将数字变量组织成组来找到其平均值。

We can find the mean of the numeric variables by organizing them to groups by using some other variables.

Example

在下面的示例中,我们找到了每个汽车品牌的每个型号的变量马力的均值。

In the example below we find the mean of the variable horsepower for each type under each make of the car.

PROC MEANS DATA = sashelp.CARS mean SUM MAXDEC=2;
class make type;
var horsepower;
RUN;

执行以上代码后,我们将得到以下输出 −

When the above code is executed, we get the following output −

mean with class