/******************************************************************
* 分层 Cox 比例风险回归完整示例：Overall Survival
* 数据: example.csv（原创模拟 ADTTE 风格数据）
* 约定: PARAMCD='OS', ANL01FL='Y', CNSR=0 事件 / CNSR=1 删失
* 模型: TRT01P + AGE，按 IxRS STRATUM1 分层，ties=Efron
* 环境: SAS 9.4
******************************************************************/

/* 1. 从当前工作目录读入示例数据 */
proc import datafile="example.csv" out=adtte dbms=csv replace;
  guessingrows=max;
run;

/* 2. 明确主分析输入范围；真实项目还应核对分析集标记 */
data adtte_os;
  set adtte;
  where upcase(paramcd)='OS' and upcase(anl01fl)='Y';
run;

/* 3. 先核对每个风险层、治疗组的事件数 */
proc freq data=adtte_os;
  tables stratum1*trt01p*cnsr / missing;
run;

/* 4. 分层 Cox 模型、HR/CI 与 PH 假设诊断 */
ods graphics on;
ods output HazardRatios=cox_hr
           ParameterEstimates=cox_parameters;

proc phreg data=adtte_os;
  class trt01p (ref='Placebo') / param=ref;
  model aval*cnsr(1) = trt01p age
        / ties=efron risklimits=wald;
  strata stratum1;                    /* 各层不同基线风险，不估计层的 HR */
  hazardratio trt01p / diff=ref cl=wald;
  assess ph / resample seed=20260821; /* Schoenfeld 残差型 PH 诊断 */
run;

ods output close;
ods graphics off;

/* 输出数据集:
   COX_HR          -> Drug A / Placebo 的 HR 与 95% Wald CI
   COX_PARAMETERS  -> 模型系数、标准误与 Wald p 值
   ASSESS PH 结果  -> 查看输出中的 supremum test 与残差图
*/
