直接输出科研
统计表格的SAS宏程序
/*ods基本语句*/
/*(1)更改输出类型*/
odslistingclose;/*关闭默认ods输出类型listing*/
odsrtffile="c:\a.rtf";/*指定ods输出类型为rtf,结果存至C盘a.rtf文件*/ ......;/*SAS过程步*/
odsrtfclose;/*关闭rtf输出类型*/
odslisting;/*恢复listing输出类型*/
/*(2)选择性输出*/
odstraceon;/*开启追踪功能,以确定SAS过程步输出结果的构成及各部分的名称*/ ......;/*SAS过程步*/
odstraceoff;/*关闭追踪功能*/
odslistingclose;/*关闭默认ods输出类型listing*/
odsoutput Aa=Bb;/*选择性地将输出结果Aa输出至SAS文件Bb*/
......;/*SAS过程步*/
odslisting;/*恢复listing输出类型*/
/*proc report基本语句*/
procreport data=a nowindows;/*调用report过程,nowindows选项用于关闭交互窗口*/ columnvar-list;/*column类似print过程的var语句,给出拟报告的变量及其顺序*/ definevar/options'column-header';/*逐一对column指定的变量的名称及格式等进行限定*/
/*示例数据集及期望生成的word表格*/
/*调用宏前的准备*/
/*定义分类变量的类别*/
/*定义数值变量的缺失值*/
/*定义基线特征变量标签*/
/*定义宏变量*/
/*
:total值拟
的数据集;group指分组变量;
cat_var指分类变量名;cat_index指基线表中该分类变量的顺次;cat_n分类变量的总数; num_var指数值变量名;num_index指基线表中该数值变量的顺次;num_n数值变量的总数;*/ %let total=a;
%let group=intervetion;
%let cat_var=%str(gender education ethnity);
%let cat_index=%str(1 3 4);
%let cat_n=3;
%let num_var=%str(age score);
%let num_index=%str(2 5);
%let num_n=2;
/*宏主
及解释*/
/*宏名称为八色,由4步生成*/
%macro base;
/*步骤1.生成分类变量的输出要素*/
/*生成频数数据集*/
%macro stepa;
datafreq_a;
set&total;
treat=(&group=1)*1+(&group=2)*0;
control=(&group=1)*0+(&group=2)*1;
run;
proc means data=freq_anoprint;
class&cat_var;
var treat control;
output out=freq_b sum=/autoname;
ways1;
run;
%global total_treattotal_control;
procsqlnoprint;
select count(*)into:total_treat from &total where &group=1;
select count(*)into:total_control from &total where &group=2;
quit;
datafreq_c;
setfreq_b;
length variable level$200.;
treat=cats(treat_sum,"(",put((treat_sum/&total_treat)*100,8.1),")"); control=cats(control_sum,"(",put((control_sum/&total_control)*100,8.1), ")");
%do i=1%to&cat_n;
if%scan(&cat_var,&i) then do;
variable="%scan(&cat_var,&i)";
index=%scan(&cat_index,&i);
level=put(%scan(&cat_var,&i),%scan(&cat_var,&i).);
end;
%end;
run;
/*输出卡方值*/
ods listing close;
ods output chisq=freq_d;
procfreq data=&total;
table&group*(&cat_var)/chisq;
run;
ods listing;
datafreq_e(keep=variable prob);
setfreq_d;
variable=scan(table,-1,"");
where statistic="chi-square" or statistic="卡方";
run;
/*合并卡方与频数数据集*/
proc sort data=freq_c;
by variable;
proc sort data=freq_e;
by variable;
run;
datafreq_f;
mergefreq_cfreq_e;
by variable;
keep variable level treat control index prob;
run;
%mend;
%stepa;
/*步骤2.生成数值变量的输出要素*/
%macro stepb;
/*参考步骤1.首先将T检验的结果以均数(
差)、方差齐性检验和t检验3个数据集存储备用,进而生成数值变量的输出要素*/
%mend;
%stepb;
/*步骤3.合并分类变量与数值变量输出要素,并按顺序变量顺序*/
%macro stepc;
datafinal;setfreq_fmean_f;
run;
proc sort data=final;
by index;
run;
%mend;
%stepc;
/*步骤4.将输出元素以rtf类型输出并存至C盘根目录下的a.rtf文件*/
%macro stepd;
optionsnodatenonumber orientation=portrait;
ods listing close;
ods rtf file="c:\a.rtf"b odytitle;
title font="宋体" height=10pt"表1 干预和对照组基线特征分布";
proc report data=final nowd style(report)={background=white rules=group frame=hsidescellwidth=60%}
style(header)={font_weight=light background=white font_size=10pt}; column variable level treat control prob;
define variable/group order=data"基线特征" style(column)={just=left}
style(header)={just=left};
define treat/display"治疗(n=%left(&total_treat))"
style(column)={just=right} style(header)={just=right};
define control/display" 对照(n=%left(&total_control))"
style(column)={just=right} style(header)={just=right};
defineprob/group"P" style(column)={just=center}
style(header)={font_style=italic};
compute before variable/style(lines)={font_size=2pt};
line"";
endcomp;
format variable $variale;
run;
ods rtf close;
ods listing;
%mend;
%stepd;
%mend base;
%base;