SAS报表 多个sheet 设定颜色(ods tagsets.excelxp;proc template;proc tabulate)
学习心得:最近发现查找SAS资料的非常方便又有效的方法:在必应网上所有要查的英文关键词,超厉害的。
目的:处理报表的输出到多个sheet中、报表指定位置加颜色
1.tabulate产生的多个报表输出到不同的sheet中,在必应网上搜索SAS tabulate sheet,第三个界面就是
这篇文章介绍了使用ODS tagsets.excelxp可以实现报表输出到多个sheet中,具体的文章和数据代码可以在support.sas.com/saspresents下载到类似的。
2.实践:拿sashelp.class中的数据做例子
(1)取数据
LIBNAME MYLIB ".\DATA";/*自己取一个位置放数据*/
DATA MYLIB.TRY;
SET SASHELP.CLASS(FIRSTOBS=1 OBS=10);
RUN;
(2)使用ODS Tagsets.ExcelXP将tabulate等(print tabulate report)输出到xml中,可以用EXCEL打开
TITLE;/*清理之前的title*/
FOOTNOTE;/*清理之前的footnote*/
ODS _ALL_ CLOSE; /*关闭之前所有的ODS设置*/
LIBNAME MYLIB ".\DATA" ACCESS=READ;
/*******************Start******************/
ODS TAGSETS.EXCELXP FILE=".\TRY.xml" STYLE=PRINTER;
TITLE 'TRY SASHELP.CLASS';
FOOTNOTE;
ODS TAGSETS.EXCELXP OPTIONS(SHEET_NAME="SEX*WEIGHT 性别体重");/*设置输出到第一个sheet中的名称,否则使用默认的名称*/
PROC TABULATE DATA=MYLIB.TRY;
CLASS SEX WEIGHT;
TABLE SEX,WEIGHT*(N*F=6.0);
RUN;
ODS TAGSETS.EXCELXP OPTIONS(SHEET_NAME="AGE*HEIGHT 年龄身高");/*设置输出到第二个sheet中的名称,否则使用默认的名称*/
PROC TABULATE DATA=MYLIB.TRY;
CLASS AGE HEIGHT;
TABLE AGE,HEIGHT*(N*F=6.0);
RUN;
ODS TAGSETS.EXCELXP CLOSE;
/**************END********/
(3)设置输出到excel中的格式颜色等,使用PROC TEMPLATE;定义style
proc template;
define style styles.SUGI31;
parent = styles.Journal;
/*****************************************************************************
Use approximate "SUGI reddish brown" cx993300 for some foreground elements.
Use black for other foreground elements.
Use approximate "SUGI tan" cxffcc99 for some background elements.
*****************************************************************************/
replace colors /
'docbg'= cxFFFFFF
'contentbg' = cxFFFFFF
'contentfg' = cx000000
'titlefg' = _undef_
'link2'=cx0066AA
'link1'=cx004488
'bg5'=cxFFFFFF
'bg4'=cxFFFFFF
'fg4'=cx000000
'bg3'=cxFFFFFF
'fg3'=cx000000
'bg2'=cxffcc99 /* Header and Row Header cells */
'bgA1'=cxFFFFFF
'fgA1'=cxff0000 /* Border lines */
'fg2'=cx993300 /* Header and Row Header cells */
'fg' =cx993300; /* Titles and Footnotes */
/* Redefine some characteristics of some of the standard styles. */
style table from table /
borderwidth = 0;
style header from header /
just=center
font_weight = bold
bordertopwidth = 3
borderbottomwidth = 3
borderleftwidth = 0
borderrightwidth = 0;
style rowheader from rowheader /
font_weight = bold;
/* Controls border lines of some Header cells. */
style header_r_border from header /
just= center
bordertopwidth= 3
borderbottomwidth = 3
borderleftwidth = 0
borderrightwidth = 3;
/* Controls border lines of the TABULATE box cell. */
style header_box from header /
just= center
bordertopwidth= 3
borderbottomwidth = 3
borderleftwidth = 0
borderrightwidth = 3;
/* Controls border lines of some Row Header cells. */
style rowheader_r_border from rowheader /
bordertopwidth= 0
borderbottomwidth = 0
borderleftwidth = 0
borderrightwidth = 3;
/* Assigns an Excel format to certain Data cells. */
style data_z8 from data /
tagattr='format:00000000';
end;
run; quit;
对应的将前面的
ODS TAGSETS.EXCELXP FILE=".\TRY.xml" STYLE=PRINTER;
改为
ODS TAGSETS.EXCELXP FILE=".\TRY.xml" STYLE=SUGI31;
效果如下:
2.设置输出的表格颜色
OPTIONS CENTER;
TITLE;
FOOTNOTE;
ODS _ALL_ CLOSE;
LIBNAME MYLIB ".\DATA" ACCESS=READ;
/*定义颜色*/
%let color1=#FFFF80; /*淡黄色*/
%let color2=#80FF80; /*浅绿色*/
%let color3=#DBFE8E; /*淡黄绿色*/
/***又一次使用template定义table****/
PROC TEMPLATE;
Define table MYTABLE;
cellstyle _row_ = 1 && _col_ = 1 as {BackgroundColor=&color1. },/*定义确定的某个表格指定颜色*/
_row_ = 1 && _col_ = 2 as {BackgroundColor=&color2. },
_row_ = 1 && _col_ = 3 as {BackgroundColor=&color3. },
_datatype_ = "num" as {color=red},
_datatype_ = "char" as {color=blue};
END;
RUN;QUIT;
PROC TABULATE DATA=MYLIB.TRY OUT=A;
CLASS SEX WEIGHT;
TABLE SEX,WEIGHT*(N*F=6.0);
RUN;
PROC TABULATE DATA=MYLIB.TRY OUT=B;
CLASS AGE WEIGHT;
TABLE AGE,WEIGHT*(N*F=6.0);
RUN;
/**********start*****将模板应用到tabulate的输出*/
ODS PDF FILE=".\DIFF_color.PDF";
DATA _NULL_;
SET A;
FILE PRINT ODS=(TEMPLATE='MYTABLE');
PUT _ODS_;
RUN;
DATA _NULL_;
SET B;
FILE PRINT ODS=(TEMPLATE='MYTABLE');
PUT _ODS_;
RUN;
ODS PDF CLOSE;
/********END********/
结果: