利用SAS宏程序和循环语句要实现自动输出cox回归单因素分析结果,目标表格如下:
在图片中
程序如下:
/*creat database and import data*/
%let m=1;/*create macro variable m (the number of Excel files)*/
%let n=14;/* create macro variable n (the number of variables to be analysed in each Excel files)*/
%let k=%eval(&m*&n);
%put &k;/* create macro variable k (the number of output results,k=m*n */
/***libname RPP 'D:\SASbase\';***/
ods listing close;
/*ods results off;*/
PROC PRINTTO PRINT='D:\SASbase\auto.lst'
LOG='D:\SASbase\auto.log' NEW;
RUN;/*output the log and result*/
%macro COX;/*create the macro COX*/
%do a=1 %to &m;/*read circularly m Excel files*/
%let x=%eval(1+(&a-1)*&n);
%put &x;/*create the macro variable x,= the first COX variable in each Excel file*/
%let y=%eval(&a*&n);
%put &y;/*create the macro variable y,= the last COX variable in each Excel file*/
%do b=&x %to &y;
/*import data file*/
proc import datafile="D:\SASbase\HCCcox&a..xls" dbms=excel out=RPP.HCCcoxm&a;
sheet='sheet1';
getnames=yes;
run;
/*univariate cox analysis */
data COX&a;
set RPP.HCCcoxm&a;
label time='Survival Time' HCCFU='0=HCCfree 1=HCC';
run;
ods output ParameterEstimates;/*output the ParameterEstimates in the phreg results*/
proc phreg data=COX&a;
model time*HCCFU(0)=COX&b;
run;
%end;
%end;
%mend;
%COX
PROC PRINTTO PRINT=PRINT LOG=LOG;
RUN;
/*output results into an excel file named “resultHCC”*/
data coxHCC;
set WORK.DATA1-WORK.DATA&k;
length Parameter $20;
run;
PROC export data=WORK.coxHCC
outfile="D:\SASbase\resultHCC.xlsx"
DBMS=Excel REPLACE;
SHEET='resultHCC';
run;
运行结果:名字不能自动循环到14,9以后就重复出现1.其他结果正确。
Parameter
COX1
COX2
COX3
COX4
COX5
COX6
COX7
COX8
COX9
COX1
COX1
COX1
COX1
COX1
如何修改程序。