|
两组间T检验SAS宏程序
collen@acrp
/* 两组之间定量指标比较t检验的统计表格 */
%macro ttest(database=,var=, index=);
* 定义"ttest"宏,其中3个宏变量&database、&var和&index;
* 宏变量&database用来指定数据集,如database=sasuser.pps;
* 宏变量&var用来指定要分析的变量,如var=height,var后只能接一个变量;
* 宏变量&index用来说明label要分析的变量,如index=体温;
proc sort data=&data.;
by group;
run;
proc univariate noprint data=&database.;
var &var.;
by group;
output out=result n=n mean=mean median=med std=std min=min max=max nmiss=nmiss
Q1=Q1 Q3=Q3;
run;
* 将描述性结果输出到result数据集中;
data a(where=(group="A")) b(where=(group="B"));
set result;
format mean 6.2 med 6.2 std 6.2;
run;
* 将两组的描述性结果分别输出到独立的数据集a、b中,并定义mean、med、std的长度;
data result(drop=group);
merge a(rename=(n=NA nmiss=MA mean=MeanA med=medA std=stdA Q1=QA1 Q3=QA3
min=minA max=maxA))
b(rename=(n=NB nmiss=MB mean=MeanB med=medB std=stdB Q1=QB1 Q3=QB3
min=minB max=maxB));
run;
* a、b分别重新对变量更名,再合并到一个数据集中;
ods listing close;
ods output ttests=ttests equality=equality;
proc ttest data=&database.;
var &var.;
class group;
run;
ods listing;
* 利用ODS将成组t检验结果输出到数据集ttests,方差齐性检验结果输出到数据集equality;
data test2;
merge ttests equality;
by variable;
* 合并数据集ttests、equality到数据集test2中;
data test21;
set test2;
where ProbF>0.05 and Variances="Equal";
length stat $5;
stat="t";
run;
* 生成方差齐性的成组t检验数据集test21,并定义变量格式;
data test22;
set test2;
where ProbF<0.05 and Variances="Unequal";
length stat $5;
stat="校正t";
run;
* 生成方差非齐性的成组t检验数据集test22,并定义变量格式;
data test(keep=tValue Probt stat);
merge test21 test22;
tValue=abs(tValue);
format tValue 6.2 Probt 6.4;
run;
* 合并成组t检验数据集test21、test22,并定义变量长度;
data _null_;
merge result test;
file "C:\1.txt" mod;
put
#1 @3 "&index." @70 stat '=' tValue @85 'P=' Probt
#2 @5 "例数(缺失)" @22 NA '(' MA ')' @47 NB '(' MB ')'
#3 @5 "均数(标准差)" @22 MeanA '(' StdA ')' @47 MeanB '(' StdB ')'
#4 @5 "中位数(P25~P75)" @22 MedA '(' QA1 '-' QA3 ')' @47 MedB '(' QB1 '-' QB3 ')'
#5 @5 "最小值-最大值" @22 MinA '-' MaxA @47 MinB '-' MaxB;
run;
* 表格基本框架构建;
data _null_;
merge result test;
file "C:\结论.txt" mod;
if Probt>0.05 then do ;
put @1 '两组间' "&index." '比较无统计学意义(P>0.05)' ;
end;
else do ;
put @1 '两组间' "&index." '比较有统计学意义(P<0.05)' ;
end ;
;
run;
proc datasets;
delete result a b TTests Equality test2 test21 test22 test stt;
quit;
* 删除临时数据集;
%mend ttest;
%macro ctit(tit);
data _null_;
file "C:\1.txt" mod;
put
#1 @3 "&tit"
#2 @2 90*'_'
#3 @3 '指标项' @22 'A组' @47 'B组' @72 '统计量' @87 'P值'
#4 @2 90*'_';
run;
%mend ctit;
* 定义表格的表头和表格线;
%macro ctit0(tit);
data _null_;
file "C:\结论.txt" mod;
put
#2 @1 "&tit"
;
run;
%mend ctit;
%macro cleg;
data _null_;
file "C:\1.txt" mod;
put #1 @2 90*'_';
run;
%mend cleg;
* 定义表格线;
/*
data a1;
input group$ tiwen xinlv @@;
cards;
A 75.2 75.1 A 76.4 80 A 80 65 A 77 82 A 80 95
A 77.1 82.2 A 73 .
B 82.2 66.9 B 80.1 74.9 B 85 70 B 85 74 B 78 99
B 87.3 80.8 B 82.2 60
;
run;
%ctit0(两组基线数据比较)
%ctit (两组基线数据比较)
%ttest(database=work.a1,var=tiwen, index=体温)
%ttest(database=work.a1,var=xinlv, index=心率)
%cleg
*/
转贴自EPIman
|
|