# SAS中每个独立观察的平均___？Average ___ per distinct observation in sas?

`````` Name:     Grade:      Subject:
Bob         8         English
Bob         6         Maths
Bill        7         English
Joe         9         Maths
``````

`````` Name:     Grade:
Bob         14
Bill        7
Joe         9
``````

## 4 个回复4

### #1楼 票数：2 已采纳

``````/*sorting*/
proc sort data=have out=have1;
by name;
run;

do until(last.name);
by name;
end;
run;
``````

``````proc sql;
create table want as
from have
group by name;
``````

### #2楼 票数：1

``````proc sort data=sashelp.class out=class(keep=Sex Name Weight);
by sex;
run;
``````

``````data res;
set class;
by sex;
retain sum 0 n 0;
sum+Weight;
n+1;

if last.sex then
do;
avg=sum/n;
n=0;
Sum=0;
end;
run;
``````

``````+---------+-----+--------+-------+---+--------------+
|  Name   | Sex | Weight |  sum  | n |     avg      |
+---------+-----+--------+-------+---+--------------+
| Alice   | F   |     84 |    84 | 1 |            . |
| Barbara | F   |     98 |   182 | 2 |            . |
| Carol   | F   |  102.5 | 284.5 | 3 |            . |
| Jane    | F   |   84.5 |   369 | 4 |            . |
| Janet   | F   |  112.5 | 481.5 | 5 |            . |
| Joyce   | F   |   50.5 |   532 | 6 |            . |
| Judy    | F   |     90 |   622 | 7 |            . |
| Louise  | F   |     77 |   699 | 8 |            . |
| Mary    | F   |    112 |     0 | 0 | 90.111111111 |
| Alfred  | M   |  112.5 | 112.5 | 1 |            . |
| Henry   | M   |  102.5 |   215 | 2 |            . |
| James   | M   |     83 |   298 | 3 |            . |
| Jeffrey | M   |     84 |   382 | 4 |            . |
| John    | M   |   99.5 | 481.5 | 5 |            . |
| Philip  | M   |    150 | 631.5 | 6 |            . |
| Robert  | M   |    128 | 759.5 | 7 |            . |
| Ronald  | M   |    133 | 892.5 | 8 |            . |
| Thomas  | M   |     85 | 977.5 | 9 |            . |
| William | M   |    112 |     0 | 0 |       108.95 |
+---------+-----+--------+-------+---+--------------+
``````

### #3楼 票数：1

``````proc sql noprint;
from have
group by Name
;
quit;
``````

### #4楼 票数：1

1. 在PROC MEANS语句上，有几个选项：
• NWAY-仅包括最高级别，否则SAS也将添加总体总计
• N / MEAN / NMISS / MEDIAN-要准备的统计数据
• STACKODS更改输出格式，这通常是大多数人想要的格式。
2. CLASS->分组变量
3. VAR->要分析的变量
4. ODS OUTPUT->将结果发送到名为WANT的表。

` `proc means data=sashelp.class NWAY N MEAN NMISS MEDIAN STACKODS; CLASS sex; *this is your grouping variable(s), ie name; VAR height; *this is the variable(s) you want to summarize; ods output summary = want; run;` `

2回复

2回复

2回复

3回复

1回复

1回复

1回复

2回复