1. 首页
  2. R语言

【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

SAS里面总结数据:MEANS

SAS当然还有类似于excel的数据透视表和R的data.table的模块,就是MEANS。可以输出的summary statistics包括最大值、最小值、平均值、中位数、余非缺失值个数、缺失值个数、范围、标准差、和等等。此外,还可以使用BY或者CLASS进行 分组统计,VAR选择变量等。

比如:

1

2

3

4

5

6

7

8

9

10

11

12

13

DATA sales;

INFILE 'c:MyRawDataFlowers.dat';

INPUT CustomerID $ @9 SaleDate MMDDYY10. Petunia SnapDragon

Marigold;

Month = MONTH(SaleDate);

PROC SORT DATA = sales;

BY Month;

* Calculate means by Month for flower sales;

PROC MEANS DATA = sales;

BY Month;

VAR Petunia SnapDragon Marigold;

TITLE 'Summary of Flower Sales by Month';

RUN;

可以实现:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Summary of Flower Sales by Month 1

Month=5

The MEANS Procedure

Variable N Mean Std Dev Minimum Maximum

Petunia 3 86.6666667 35.1188458 50.0000000 120.0000000

SnapDragon 3 113.3333333 41.6333200 80.0000000 160.0000000

Marigold 3 81.6666667 25.6580072 60.0000000 110.0000000

Month=6

Variable N Mean Std Dev Minimum Maximum

Petunia 4 81.2500000 16.5201897 60.0000000 100.0000000

SnapDragon 4 97.5000000 47.8713554 60.0000000 160.0000000

Marigold 4 83.7500000 19.7378655 60.0000000 100.0000000

当然这些统计量也可以直接的写入一个SAS数据表,只需要加上一个OUTPUT就可以了。原数据:

1

2

3

4

5

6

7

75601 05/04/2008 120 80 110

83401 05/12/2008 90 160 60

90102 05/18/2008 50 100 75

83401 06/01/2008 80 60 100

75601 06/11/2008 100 160 75

90102 06/19/2008 60 60 60

75601 06/25/2008 85 110 100

SAS代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

DATA sales;

INFILE 'c:MyRawDataFlowers.dat';

INPUT CustomerID $ @9 SaleDate MMDDYY10. Petunia SnapDragon Marigold;

PROC SORT DATA = sales;

BY CustomerID;

* Calculate means by CustomerID, output sum and mean to new data set;

PROC MEANS NOPRINT DATA = sales;

BY CustomerID;

VAR Petunia SnapDragon Marigold;

OUTPUT OUT = totals MEAN(Petunia SnapDragon Marigold) =

MeanPetunia MeanSnapDragon MeanMarigold

SUM(Petunia SnapDragon Marigold) = Petunia SnapDragon Marigold;

PROC PRINT DATA = totals;

TITLE 'Sum of Flower Data over Customer ID';

FORMAT MeanPetunia MeanSnapDragon MeanMarigold 3.;

RUN;

最终结果为:

【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

SAS PROC统计频率:FREQ

计数的话,就要靠SAS里面的FREQ模块了。比如我们有一个数据集:

1

2

3

esp w cap d cap w kon w ice w kon d esp d kon w ice d esp d

cap w esp d cap d Kon d . d kon w esp d cap w ice w kon w

kon w kon w ice d esp d kon w esp d esp w kon w cap w kon w

然后可以用FREQ来统计一些基本量:

1

2

3

4

5

6

7

DATA orders;

INFILE 'c:MyRawDataCoffee.dat';

INPUT Coffee $ Window $ @@;

* Print tables for Window and Window by Coffee;

PROC FREQ DATA = orders;

TABLES Window Window * Coffee;

RUN;

最终会得到一个2×5的表格:

【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

SAS PROC汇报表格:TABULATE

基本看到TABULATE就可以想到那个著名的软件Tabular了…不过貌似SAS也自带了一个类似的表格模块。这个东西可以变得非常复杂,不过鉴于我一时半会儿还用不到,所以也没有细细看。抄个例子吧。

原数据:

1

2

3

4

5

6

7

8

9

10

Silent Lady Maalea sail sch 75.00

America II Maalea sail yac 32.95

Aloha Anai Lahaina sail cat 62.00

Ocean Spirit Maalea power cat 22.00

Anuenue Maalea sail sch 47.50

Hana Lei Maalea power cat 28.99

Leilani Maalea power yac 19.99

Kalakaua Maalea power cat 29.50

Reef Runner Lahaina power yac 29.95

Blue Dolphin Maalea sail cat 42.95

SAS代码:

1

2

3

4

5

6

7

8

9

10

11

DATA boats;

INFILE 'c:MyRawDataBoats.dat';

INPUT Name $ 112 Port $ 1420 Locomotion $ 2226 Type $ 2830

Price 3236;

RUN;

* Tabulations with three dimensions;

PROC TABULATE DATA = boats;

CLASS Port Locomotion Type;

TABLE Port, Locomotion, Type;

TITLE 'Number of Boats by Port, Locomotion, and Type';

RUN;

最终结果:【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

类似的,还可以增加统计量(类似于MEANS那里):

1

2

3

4

5

6

7

8

9

10

11

12

13

DATA boats;

INFILE 'c:MyRawDataBoats.dat';

INPUT Name $ 112 Port $ 1420 Locomotion $ 2226 Type $ 2830

Price 3236;

RUN;

* PROC TABULATE report with options;

PROC TABULATE DATA = boats FORMAT=DOLLAR9.2;

CLASS Locomotion Type;

VAR Price;

TABLE Locomotion ALL, MEAN*Price*(Type ALL)

/BOX='Full Day Excursions' MISSTEXT='none';

TITLE;

RUN;

可以得到:【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

最后还可以混合FORMAT等等,可以变得相当的复杂。貌似这东西是美国劳工部鼓捣出来的格式…

1

2

3

4

5

6

7

8

9

10

11

12

13

DATA boats;

INFILE 'c:MyRawDataBoats.dat';

INPUT Name $ 112 Port $ 1420 Locomotion $ 2226 Type $ 2830

Price 3236 Length 3840;

RUN;

* Using the FORMAT= option in the TABLE statement;

PROC TABULATE DATA = boats;

CLASS Locomotion Type;

VAR Price Length;

TABLE Locomotion ALL,

MEAN * (Price*FORMAT=DOLLAR6.2 Length*FORMAT=6.0) * (Type ALL);

TITLE 'Price and Length by Type of Boat';

RUN;

BOSS级汇报表格呈现了…

【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

我只能感慨,不愧是商业软件啊,用户需求考虑的真的是特别的周到…这种费时费力做汇报表格的事情也被搞定了,强悍。

SAS里面的报告:REPORT

还有一个REPORT,看到有TABULATE的时候我已经不奇怪并略略的有些期待一个做报告的模块出现了。这东西基本就是前面几个的超级混合体,反正你想搞到的汇报模式总是能够搞出来的。

又是一堆数据:

1

2

3

4

5

17 sci 9 bio 28 fic 50 mys 13 fic 32 fic 67 fic 81 non 38 non

53 non 16 sci 15 bio 61 fic 52 ref 22 mys 76 bio 37 fic 86 fic

49 mys 78 non 45 sci 64 bio 8 fic 11 non 41 fic 46 ref 69 fic

34 fic 26 mys 23 sci 74 ref 15 sci 27 fic 23 mys 63 fic 78 non

40 bio 12 fic 29 fic 54 mys 67 fic 60 fic 38 sci 42 fic 80 fic

然后一堆SAS代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

DATA books;

INFILE 'c:MyRawDataLibraryBooks.dat';

INPUT Age BookType $ @@;

RUN;

*Define formats to group the data;

PROC FORMAT;

VALUE agegpa

018 = '0 to 18'

1925 = '19 to 25'

2649 = '26 to 49'

50HIGH = ' 50+ ';

VALUE agegpb

025 = '0 to 25'

26HIGH = ' 26+ ';

VALUE $typ

'bio','non','ref' = 'Non-Fiction'

'fic','mys','sci' = 'Fiction';

RUN;

*Create two way table with Age grouped into four categories;

PROC FREQ DATA = books;

TITLE 'Patron Age by Book Type: Four Age Groups';

TABLES BookType * Age / NOPERCENT NOROW NOCOL;

FORMAT Age agegpa. BookType $typ.;

RUN;

*Create two way table with Age grouped into two categories;

PROC FREQ DATA = books;

TITLE 'Patron Age by Book Type: Two Age Groups';

TABLES BookType * Age / NOPERCENT NOROW NOCOL;

FORMAT Age agegpb. BookType $typ.;

RUN;

然后一堆交叉计数的结果就出来了:【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

当然,简单的计算和分类统计也不在话下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

DATA natparks;

INFILE 'c:MyRawDataParks.dat';

INPUT Name $ 121 Type $ Region $ Museums Camping;

RUN;

*Statistics in COLUMN statement with two group variables;

PROC REPORT DATA = natparks NOWINDOWS HEADLINE;

COLUMN Region Type N (Museums Camping),MEAN;

DEFINE Region / GROUP;

DEFINE Type / GROUP;

TITLE 'Statistics with Two Group Variables';

RUN;

*Statistics in COLUMN statement with group and across variables;

PROC REPORT DATA = natparks NOWINDOWS HEADLINE;

COLUMN Region N Type,(Museums Camping),MEAN;

DEFINE Region / GROUP;

DEFINE Type / ACROSS;

TITLE 'Statistics with a Group and Across Variable';

RUN;

可以得到一个看起来很fancy的表格:【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

SAS数据总结综述

我的感觉是,MEANS, TABULATE和REPORT这三个模块各有千秋,基本就是可以替代EXCEL的数据透视表,虽然效率上说不好谁比谁高…随便哪一个用习惯了就好,反正又不是天天出政府报告的,我就懒得深究了。

原文始发于微信公众号(PPV课数据科学社区):【学习】七天搞定SAS(三):基本模块调用(格式、计数、概要统计、排序等)(下)

原创文章,作者:ppvke,如若转载,请注明出处:http://www.ppvke.com/archives/30997

联系我们

4000-51-9191

在线咨询:点击这里给我发消息

工作时间:周一至周五,9:30-18:30,节假日休息