一、Proc report介绍
ODS样式是控制SAS输出表和图的整体外观,可以指定图形元素的颜色、字体、线条样式、符号标记和其他属性。
ODS样式有:ODS HTML、ODS PDF、ODS TRF等。
ODS RTF可以通过Proc print、Proc tabulate、Proc report定义输出,下面主要介绍Proc report过程。
Proc report可以定义SAS数据集中变量输出的格式,以及输出样式等。
二、Proc report语法
PROC REPORT DATA=dataset options;
columns var_1,...,var_n;
define var_1;
define var_2;
...;
define var_n;
COMPUTE blocks
BREAK ...;
RBREAK ...;
run;
三、简单报表:各样式的定义
1、简单输出:定义变量的输出格式
ods rtf file="D:\sas\example1.rtf";
proc report data=sashelp.class headline headskip;
column name sex age height weight;
define name/display width=12 '姓名'; /*定义name变量的属性
width=12定义的是长度????????
‘姓名’定义的是输出到表格的lable,
若不定义则为数据集中默认的lable*/
define sex/display;
define age/display format=8. width=10; /*format=8. 定义age的输出格式是数值型并非整形*/
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
run;
ods rtf close;
2、Title定义
使用style(report)定义的是表格的标题
用Title和Title1等参数定义的是页眉中的标题
ods rtf file="D:\sas\example2.rtf";
proc report data=sashelp.class headline headskip
style(report)=[pretext="简单报表"
just=center
font_size=12pt
font_face="宋体"
font_weight=bold]; /*定义是表格的标题*/
Title1 "简单报表"; /*定义是页眉中的标题*/
column name sex age height weight;
define name/display width=12 '姓名';
define sex/display;
define age/display format=8. width=10;
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
run;
ods rtf close;
3、去掉SAS自带的元素输出以及定义纸张方向
orientation=landscape定义页面的输出方向,landscape:横向,portrait:纵向
通过OPTIONS去掉了SAS系统输出的日期,页码。
title; /*清空页眉中的title*/
options nodate nonumber orientation=landscape missing="";
ods rtf file="D:\sas\example3.rtf";
proc report data=sashelp.class headline headskip
style(report)=[pretext="简单报表"
just=center
font_size=12pt
font_face="宋体"
font_weight=bold]; /*定义是表格的标题*/
column name sex age height weight;
define name/display width=12 '姓名';
define sex/display;
define age/display format=8. width=10;
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
run;
ods rtf close;
四、三线表输出
1、SAS自带的三线表输出
打印结果中红色箭头部分,是什么原因我不记得了,请路过的小伙伴儿指导以下~多谢多谢!
title; /*清空页眉中的title*/
options nodate nonumber orientation=portrait missing="";
ods rtf file="F:\sas\example4.rtf" style=Journal; /*journal是SAS本身自带的格式输出*/
proc report data=sashelp.class headline headskip nowd
style(report)=[pretext=%str("三线表输出")
just=center
font_size=10pt
font_face="Roman"
font_weight=bold];
column name sex age height weight;
define name/display width=12 '姓名';
define sex/display;
define age/display format=8. width=10;
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
run;
ods rtf close;
2、自定义三线表的输出样式(一)
使用自定义的三线表输出样式
Proc template;
define style journalTest;
parent = styles.rtf;
style table /
just=center /*居中对齐*/
vjust=top /*位置定义*/
cellspacing=0 /*单元格间距*/
cellpadding=2 /*单元格边距*/
frame=hsides /*框架类型*/
rules=groups;
end;
run;
title;
options nodate nonumber orientation=portrait missing="";
ods rtf file="F:\sas\example5.rtf" style=JournalTest; /*JournalTest是SAS本身自带的格式输出*/
proc report data=sashelp.class headline headskip nowd
style(report)=[pretext=%str("三线表输出")
just=center
font_size=10pt
font_face="Roman"
font_weight=bold];
column name sex age height weight;
define name/display width=12 '姓名';
define sex/display;
define age/display format=8. width=10;
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
run;
ods rtf close;
3、自定义三线表的输出样式(二)
Proc template;
define style journalTest;
parent = styles.rtf;
class fonts /
"DocFont" = ("Times New Roman", 10pt) /*单元格*/
"EmphasisFont" = ("Times New Roman", 10pt, Bold) /*线条*/
"HeadingFont" = ("Times New Roman", 10pt, Bold); /*表头*/
class header /
background = white
just=center;
style table /
just=center /*居中对齐*/
vjust=top /*位置定义*/
cellspacing=0 /*单元格间距*/
cellpadding=2 /*单元格边距*/
frame=hsides /*框架类型*/
rules=groups
outputwidth = 80%;
style Cell /
just = center
font_face="Times New Roman"
font_size=10pt;
end;
run;
五、定义表格的脚注
Proc template;
define style journalTest;
parent = styles.rtf;
class fonts /
"DocFont" = ("Times New Roman", 10pt) /*单元格*/
"EmphasisFont" = ("Times New Roman", 10pt, Bold) /*线条*/
"HeadingFont" = ("Times New Roman", 10pt, Bold); /*表头*/
class header /
background = white
just=center;
style table /
just=center /*居中对齐*/
vjust=top /*位置定义*/
cellspacing=0 /*单元格间距*/
cellpadding=2 /*单元格边距*/
frame=hsides /*框架类型*/
rules=groups
outputwidth = 80%;
style Cell /
just = center
font_face="Times New Roman"
font_size=10pt;
class systemfooter /
font_size=10pt
just = left
textindent=1.4in;
end;
run;
ods escapechar="^";
title;
footnote;
options nodate nonumber orientation=portrait missing=" ";
ods rtf file="F:\sas\example7.rtf" style=journalTest; /*journalTest是SAS本身自带的格式输出*/
proc report data=sashelp.class headline headskip nowd
style(report)=[pretext=%str("三线表输出")
just=center
font_size=10pt
font_face="Roman"
font_weight=bold];
column name sex age height weight;
define name/display width=12 '姓名';
define sex/display;
define age/display format=8. width=10;
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
run;
ods rtf text="^S={outputwidth=100% just=l} 注:脚注的定义 ";
ods rtf close;
六、对某一行添加横线
ods escapechar="^";
title;
footnote;
options nodate nonumber orientation=portrait missing=" ";
ods rtf file="D:\sas\example7.rtf" style=journalTest; /*journalTest是SAS本身自带的格式输出*/
proc report data=sashelp.class headline headskip nowd
style(report)=[pretext=%str("三线表输出")
just=center
font_size=10pt
font_face="Roman"
font_weight=bold];
column name sex age height weight;
define name/display width=12 '姓名';
define sex/display;
define age/display format=8. width=10;
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
compute name;
if name="简" then
call define ( _row_,'style','style=[bordertopcolor=black bordertopwidth=100%]');
endcomp;
run;
ods rtf text="^S={outputwidth=100% just=l} 注:脚注的定义 ";
ods rtf close;
七、对某个单元格进行颜色标记
ods escapechar="^";
title;
footnote;
options nodate nonumber orientation=portrait missing=" ";
ods rtf file="D:\sas\example7.rtf" style=journalTest; /*journalTest是SAS本身自带的格式输出*/
proc report data=sashelp.class headline headskip nowd
style(report)=[pretext=%str("三线表输出")
just=center
font_size=10pt
font_face="Roman"
font_weight=bold];
column name sex age height weight;
define name/display width=12 '姓名';
define sex/display;
define age/display format=8. width=10;
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
compute age;
if age=16 then
call define ('age','style','style=[color=red]');
endcomp;
run;
ods rtf text="^S={outputwidth=100% just=l} 注:脚注的定义 ";
ods rtf close;
八、添加页码
ods escapechar="^";
title;
footnote justify=right '第^{thispage}页/共^{lastpage}页';
options nodate nonumber orientation=portrait missing=" ";
ods rtf file="D:\sas\example7.rtf" style=JournalTest; /*JournalTest是SAS本身自带的格式输出*/
proc report data=sashelp.class headline headskip nowd
style(report)=[pretext=%str("三线表输出")
just=center
font_size=10pt
font_face="Roman"
font_weight=bold];
column name sex age height weight;
define name/display width=12 '姓名';
define sex/display;
define age/display format=8. width=10;
define height/display format=6.2 width=10;
define weight/display format=6.2 width=10;
run;
ods rtf text="^S={outputwidth=100% just=l} 注:脚注的定义 ";
ods rtf close;
九、表格输出
data test;
input group $ sex $ class $ count;
cards;
A 男 Y 12
A 女 N 10
A 女 Y 11
A 男 N 18
B 女 Y 13
B 女 N 14
B 男 N 17
B 男 Y 11
;
run;
ods escapechar="^";
title;
footnote;
options nodate nonumber orientation=portrait missing=" ";
ods rtf file="D:\sas\example11.rtf" style=JournalTest; /*JournalTest是SAS本身自带的格式输出*/
proc report data=test headline headskip nowd
style(report)=[pretext=%str("各类表格输出1")
just=center
font_size=10pt
font_face="Roman"
font_weight=bold];
column group sex class count;
define group/order width=12 '分组';
define sex/order '性别';
define class/order width=10 '是否回答';
define count/format=6.2 width=10 '频数';
compute before group;
line '';
endcomp;
run;
ods rtf close;
十、表格输出2
data test;
input class1 $ class2 $ group1 $ group2 $;
cards;
性别 男 5(33.3%) 6(46.2%)
性别 女 10(66.7%) 7(53.8%)
年龄 n 15 13
年龄 Mean 50.7 54.9
年龄 Median 50.0 56.0
年龄 STD 9.20 7.22
年龄 Min,Max 37,69 42,67
;
run;
ods escapechar="^";
title;
footnote;
options nodate nonumber orientation=portrait missing=" ";
ods rtf file="D:\sas\example12.rtf" style=journalTest; /*journalTest是SAS本身自带的格式输出*/
proc report data=test headline headskip nowd
style(report)=[pretext=%str("各类表格输出2")
just=center
font_size=10pt
font_face="Roman"
font_weight=bold];
column class1 class2 ("^S={textdecoration=underline}分组" group1 group2);
define class1/order order=data noprint;
define class2/"分类" style(header)=[just=l indent=0 in]
style(column)=[just=l cellwidth=2.8 in indent=.25 in];
define group1/"实验组| (N=15)";
define group2/"对照组| (N=13)";
compute before class1;
line " ";
line @1 class1 $16.;
endcomp;
run;
run;
ods rtf close;
十一、图表一
ods escapechar="^";
title;
footnote;
options nodate nonumber orientation=portrait missing=" ";
ods rtf file="D:\sas\example13.rtf";
proc sgplot data=sashelp.cars;
hbox weight / category=origin;
run;
ods rtf close;
十二、图表二
ods escapechar="^";
ods graphics on;
ods output Survivalplot=SurvivalPlotData;
proc lifetest data=sashelp.BMT plots=survival(atrisk=0 to 2500 by 500);
time T*Status(0);
strata Group/test=logrank adjust=sidak;
run;
ods graphics off;
title;
footnote;
options nodate nonumber orientation=portrait missing=" ";
ods rtf file="D:\sas\example14.rtf";
proc sgplot data=SurvivalPlotData;
step x=time y=survival / group=stratum lineattrs=(pattern=solid thickness=2)
name='s' curvelable splitchar='-';
scatter x=time y=censored / markerattrs=(symbol=circlefilled) name='c';
scatter x=time y=censored / markerattrs=(symbol=circlefilled) GROUP=stratum;
xaxistable atrisk / x=tatrisk location=outside class=stratum colorgroup=stratum
valueattrs=(size=8) labelattrs=(size=8);
keylegend 'c' / location=inside position=topright;
run;
ods rtf close;