proc tabulate 同样可以用来生成统计量,proc tabulate 的基本语句格式:
proc tabulate ;
class classification variable list;(告诉SAS分类变量名将数据依此分类);
table page-dimension,row-dimension,columnn-dimension;(告诉SAS 如何组织表形式,以及计算哪些内容,一张table 生成一张表,可以有多个table 语句同时生成多张表);
run;
SAS 默认对class 语句所含变量分类的每类简单计数;
table 语句只输入一个dimension仅表示column dimension ;输入2个dimension分别表示 row dimension 和column dimension;输入三个dimension分别表示page dimension,row dimension,column dimension;
在class 语句的变量里缺失值会被剔除,在proc tabulate 后加入missing 可保留该缺失值,
proc tabulate missing;
proc tabulate ;
var variable list ;(告诉SAS 连续变量)
class varible list;(告诉SAS 分类变量)
table page,row,column;(table里面的变量必须来源于class 或var 变量list)
run;
每一个dimension除了包括dimension variable,还可以包括一些计算统计量的字段:
ALL 计算row,column或者page的总数
max 最大值、min 最小值、mean 算术均数、median 中位数、mode 众数、n 非缺失值个数
nmiss 缺失值个数、pctn 某组百分比、pctnsum 某组总百分比、stddev 标准误、sum 求和
变量名和统计量可以并联或并排显示concatenating 用空格分隔表现、交叉显示crossing用*表现,或者grouping用括号括起需要归纳执行同一操作的变量名或者统计量名称,
示例:
concatenating: Table locomotion type ALL;
crossing: Table mean*price;
concatenating grouping,crossing: table pctn*(locomotion type)
如:
proc tabulate data=boate;
class location type;
var price;
tabulate location all ,mean*price*(type all);
run;
结果如下:
在proc tabulate 后增加option:format=option ;format=comma6.2;可修改输出格式
在table 语句后增加option:box= 可在最左上角格子空内写入字符,标记表格,misstext=可将空缺值填入可读性更高的内容:
table region,mean*sales/box="mean sales by region" misstext="no sales";
对于class variable list ,如果要修改其作为标题的变量值的话则可以使用format 语句来重新定义变量值的内容;
但是如果需要修改作为标题的变量名称或者统计量名称,则可以使用变量名=“new name ”方式修改;当new name为空则该标题行空缺,如果是列标题行则默认删除该空行;
若为行标题行(row header),若是class variable则该空缺的行标题行会默认删除,若非class variable则空缺的行标题行(row header)会保留,若想强制删除该空缺行可以使用row=float.
proc format ;
value $typ "cat"="catamaran"
"sch"="schooner"
"yac"="yacht";
run;
proc tabulate data=boat format=dollar9.2;
class locomotion type;
var price;
format type $typ.;
table locomotion=" " ALL ,mean=" "*price="mean price by type of boat"*(type=" " all)/
box="Full Day Excursion" misstext="none";
title;
run;
也同样可以使用keylabel 或者label来修改统计量或变量名称标签,但是会置空行
可以将format 与table语句连用只修改某一特定变量结果的format,在proc tabulate内定义的format 会改变所有的表格内容format.格式如下:
variablename*format=formatw.d
table region ,mean*(sale*format=comma8.0 profit*format=dollar10.0);
proc tabulate data=boat;
class locomotion type;
var price length;
table locomotion ALL,mean*(price*format=dollar7.2 length*format=2.0)*(type ALL);
title “price and length by type of boat”;
run;