You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PROC LIFETEST的Kaplan-Meier曲线及数据导出至Excel?

问题描述

尝试导出SAS中PROC LIFETEST生成的Kaplan-Meier曲线及相关数据时,运行以下代码出现错误:

proc lifetest data=Xen plots=survival(pvalue atrisk= 0 to 12 by 2);
title 'Percentage pts with IOP <=14 Complete Success';
time Months*CS14(0);
strata Tx / test=(logrank);

ods output summary = IOP_14_CS;
proc export data=IOP_14_CS outfile='/home/u61695399/Results/3_KM_Curves.xlsx' dbms=xlsx replace;
sheet="IOP_14_CS";run;

开启ODS Trace后,日志提示输出对象'summary'未创建、WORK.MEANS.DATA不存在。需求如下:

  • 将PROC LIFETEST生成的表格及关联图形导出到Excel对应工作表
  • 多个PROC LIFETEST和PROC PHREG过程的输出,分别保存到不同命名的Excel工作表中

ODS Trace输出及错误日志

Output Added:
-------------
Name:       ProductLimitEstimates
Label:      Product-Limit Estimates
Template:   Stat.Lifetest.ProductLimitEstimates
Path:       Lifetest.Stratum1.ProductLimitEstimates
-------------

Output Added:
-------------
Name:       Quartiles
Label:      Quartiles of the Survival Distribution
Template:   Stat.Lifetest.Quartiles
Path:       Lifetest.Stratum1.TimeSummary.Quartiles
-------------

Output Added:
-------------
Name:       Means
Label:      Mean
Template:   Stat.Lifetest.Means
Path:       Lifetest.Stratum1.TimeSummary.Means
-------------

Output Added:
-------------
Name:       ProductLimitEstimates
Label:      Product-Limit Estimates
Template:   Stat.Lifetest.ProductLimitEstimates
Path:       Lifetest.Stratum2.ProductLimitEstimates
-------------

Output Added:
-------------
Name:       Quartiles
Label:      Quartiles of the Survival Distribution
Template:   Stat.Lifetest.Quartiles
Path:       Lifetest.Stratum2.TimeSummary.Quartiles
-------------

Output Added:
-------------
Name:       Means
Label:      Mean
Template:   Stat.Lifetest.Means
Path:       Lifetest.Stratum2.TimeSummary.Means
-------------
NOTE: 14460 observations with invalid time, censoring, or strata values were deleted.

Output Added:
-------------
Name:       CensoredSummary
Label:      Censored Summary
Template:   Stat.Lifetest.CensoredSummary
Path:       Lifetest.CensoredSummary
-------------

Output Added:
-------------
Name:       HomStats
Label:      Rank Statistics
Template:   Stat.Lifetest.HomStats
Path:       Lifetest.StrataHomogeneity.HomStats
-------------

Output Added:
-------------
Name:       LogrankHomCov
Label:      Log-Rank Covariance
Template:   Stat.Lifetest.HomCov
Path:       Lifetest.StrataHomogeneity.LogrankHomCov
-------------

Output Added:
-------------
Name:       HomTests
Label:      Homogeneity Tests
Template:   Stat.Lifetest.HomTests
Path:       Lifetest.StrataHomogeneity.HomTests
-------------

Output Added:
-------------
Name:       SurvivalPlot
Label:      Survival Curves
Template:   Stat.Lifetest.Graphics.ProductLimitSurvival
Path:       Lifetest.SurvivalPlot
-------------
WARNING: Output 'summary' was not created.  Make sure that the output object name, label, or path is spelled correctly.  Also, 
         verify that the appropriate procedure options are used to produce the requested output object.  For example, verify that 
         the NOPRINT option is not used.
NOTE: PROCEDURE LIFETEST used (Total process time):
      real time           2.07 seconds
      user cpu time       1.60 seconds
      system cpu time     0.08 seconds
      memory              21420.37k
      OS Memory           51388.00k
      Timestamp           2022-09-15 06:48:09 PM
      Step Count                        167  Switch Count  6
      Page Faults                       7
      Page Reclaims                     4595
      Page Swaps                        0
      Voluntary Context Switches        2204
      Involuntary Context Switches      4
      Block Input Operations            33096
      Block Output Operations           3160
      

380        proc export data=Means outfile='/home/u61695399/Results/3_KM_Curves.xlsx' dbms=xlsx replace;
381        sheet="IOP_14_CS";run;

ERROR: File WORK.MEANS.DATA does not exist.
NOTE: The SAS System stopped processing this step because of errors.

解决方案

1. 修正ODS Output对象名称错误

从ODS Trace结果可以看到,PROC LIFETEST没有生成名为summary的输出对象,必须使用实际存在的对象名捕获数据,常用对象包括:

  • 生存曲线估计值:ProductLimitEstimates
  • 生存分布四分位数:Quartiles
  • 生存时间均值:Means
  • 删失情况汇总:CensoredSummary
  • 组间一致性检验结果:HomTests

示例代码(捕获多个输出到不同数据集):

proc lifetest data=Xen plots=survival(pvalue atrisk= 0 to 12 by 2);
title 'Percentage pts with IOP <=14 Complete Success';
time Months*CS14(0);
strata Tx / test=(logrank);

/* 捕获多个输出对象到WORK库数据集 */
ods output ProductLimitEstimates = IOP_14_CS_est
           Means = IOP_14_CS_means
           HomTests = IOP_14_CS_logrank
           CensoredSummary = IOP_14_CS_censor;
run;

2. 导出多个数据集到Excel不同工作表

使用PROC EXPORT时,去掉replace参数(避免覆盖整个文件),通过指定不同sheet名称,将多个数据集存入同一Excel的不同工作表:

/* 导出生存估计值到指定工作表 */
proc export data=IOP_14_CS_est 
            outfile='/home/u61695399/Results/3_KM_Curves.xlsx' 
            dbms=xlsx;
sheet="IOP_14_CS_est";
run;

/* 导出均值结果到指定工作表 */
proc export data=IOP_14_CS_means 
            outfile='/home/u61695399/Results/3_KM_Curves.xlsx' 
            dbms=xlsx;
sheet="IOP_14_CS_means";
run;

/* 导出Logrank检验结果到指定工作表 */
proc export data=IOP_14_CS_logrank 
            outfile='/home/u61695399/Results/3_KM_Curves.xlsx' 
            dbms=xlsx;
sheet="IOP_14_CS_logrank";
run;

3. 导出Kaplan-Meier曲线到Excel

SAS无法直接将ODS图形导出到Excel单元格,但可以通过ODS EXCEL直接生成包含表格和图形的Excel文件,且支持分工作表存储不同PROC的输出:

/* 生成包含所有分析结果的Excel文件 */
ods excel file='/home/u61695399/Results/All_Analysis.xlsx';

/* 第一个PROC LIFETEST输出到指定工作表 */
ods excel options(sheet_name="IOP_14_CS");
proc lifetest data=Xen plots=survival(pvalue atrisk= 0 to 12 by 2);
title 'Percentage pts with IOP <=14 Complete Success';
time Months*CS14(0);
strata Tx / test=(logrank);
run;

/* 第二个PROC LIFETEST输出到新工作表 */
ods excel options(sheet_name="IOP_18_CS");
proc lifetest data=Xen plots=survival(pvalue atrisk= 0 to 12 by 2);
title 'Percentage pts with IOP <=18 Complete Success';
time Months*CS18(0);
strata Tx / test=(logrank);
run;

/* PROC PHREG输出到新工作表 */
ods excel options(sheet_name="PHREG_Analysis");
proc phreg data=Xen;
model Months*CS14(0) = Tx Age Gender;
run;

ods excel close;

4. 注意事项

  • 严格匹配ODS Trace输出的对象名称,SAS对大小写不敏感但名称不能出错
  • 使用ODS EXCEL时,每个ods excel options(sheet_name=...)指令会新建一个工作表,所有后续PROC输出都会写入该表
  • 若需单独处理数据集,优先用ods output捕获到WORK库,再通过proc export导出,灵活性更高

内容的提问来源于stack exchange,提问作者2567655222

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 10:15:40