Hello,
I am using complex survey data from NHANES to develop a risk prediction model. I'm following example code for non-survey data found here: https://ncook.bwh.harvard.edu/sas-macros.html
The first step is to output predicted probabilities from my Cox regression. However, the output options from PROC PHREG do not seem to be available for PROC SURVEYPHREG. I'm specifically trying output predicted survival probabilities (SURVIVAL=) and linear predictor (XBETA=).
Does anyone have advice on how to output these from PROC SURVEYPHREG?
Thanks,
Sophie
Example using PROC PHREG
/****** MACRO DEFINITION *******/
%macro PREDMAC (DSNAME, EVENT, EVNTLABL, PYRS, MODLABL, PROB);
proc phreg data=&DSNAME outest=betas(drop=_ties_ _type_ _name_);
id _id_;
model &PYRS*&EVENT(0)= &MODVARS / rl;
output out=survdat survival=survest xbeta=bx;
title2 "PHREG for Outcome: &EVNTLABL";
title3 "For Model = &MODLABL";
run;
data base; set survdat;
* Keep dummy obs only;
if _id_=0;
* baseline survival estimate for referent person;
basesurv=survest;
* centered linear estimate;
bxave=bx;
keep basesurv bxave;
put _id_= &pyrs= basesurv= bxave= ;
data preddat; merge &DSNAME survdat; by _id_;
* delete dummy obs;
if _id_=0 then delete;
data preddat;
if _n_=1 then set base;
set preddat ;
surv=basesurv**(exp(bx-bxave));
&prob=1-surv;
run;
proc univariate plot data=preddat; id _id_;
var &prob;
title2 "Predicted Event Probabilities from Cox Model";
title3 "For Outcome = &EVNTLABL and Model = &MODLABL ";
run;
%mend PREDMAC;
* EXAMPLE USAGE OF MACROS;
* %predmac(one, cvd, Total CVD, pyrs, Model with X1-X20, prcvd10);
/****** END MACRO DEFINITION *******/
Attempt to convert to PROC SURVEYPHREG:
/****** MACRO DEFINITION *******/
%macro PREDMAC (DSNAME, EVENT, EVNTLABL, PYRS, MODLABL, PROB);
proc surveyphreg data=&DSNAME ORDER=INTERNAL varmethod=taylor nomcar;
weight WTSSCB6YR;
cluster sdmvpsu;
strata sdmvstra;
domain pop;
model &PYRS*&EVENT(0)= &MODVARS / rl;
*outest=betas(drop=_ties_ _type_ _name_);
output out=survdat survival=survest xbeta=bx;
title2 "PHREG for Outcome: &EVNTLABL";
title3 "For Model = &MODLABL";
run;
data base; set survdat;
* Keep dummy obs only;
if _id_=0 and pop=1;
* baseline survival estimate for referent person;
basesurv=survest;
* centered linear estimate;
bxave=bx;
keep basesurv bxave;
put _id_= &pyrs= basesurv= bxave= ;
data preddat; merge &DSNAME survdat; by _id_;
* delete dummy obs;
if _id_=0 or pop=0 then delete;
data preddat;
if _n_=1 then set base;
set preddat ;
surv=basesurv**(exp(bx-bxave));
&prob=1-surv;
run;
proc univariate plot data=preddat; id _id_;
var &prob;
title2 "Predicted Event Probabilities from Cox Model";
title3 "For Outcome = &EVNTLABL and Model = &MODLABL ";
run;
%mend PREDMAC;
* EXAMPLE USAGE OF MACROS;
* %predmac(one, cvd, Total CVD, pyrs, Model with X1-X20, prcvd10);
/****** END MACRO DEFINITION *******/
The SURVEY procs pretty much rely on ODS OUTPUT to create data sets for the various output displayed tables.
If your Proc Surveyphreg is generating the output you need then you would add ODS OUTPUT tablename=desired_dataset; as part of the procedure code. If you don't know the names of the tables to reference you can look at the online help for the procedure in the DETAILS tab where there is a section on ODS Table Names and the options that create the tables.
OR you can add:
Ods trace on; <your procedure code goes here> ods trace off;
The LOG will then show the names of the tables used so you can add the ODS OUTPUT.
This is an example of what the LOG contents from ODS TRACE looks like from Proc Univariate code used in the ODS TRACE documentation:
Output Added: ------------- Name: Moments Label: Moments Template: base.univariate.Moments Path: Univariate.CityPop_90.Moments Label Path: "The Univariate Procedure"."CityPop_90"."Moments" ------------- Output Added: ------------- Name: BasicMeasures Label: Basic Measures of Location and Variability Template: base.univariate.Measures Path: Univariate.CityPop_90.BasicMeasures Label Path: "The Univariate Procedure"."CityPop_90"."Basic Measures of Location and Variability"
The bit you need to create a data set is the Name above. So direct the results of the Moments table to a data set I would add the proc univariate code some like this to create Work.Mymomentdataset.
ods output moments=work.mymomentsdataset;
I suggest doing the bit with ODS TRACE ON / OFF outside of a macro so you know exactly which variables are used and appear in which output sets.
The structure of the ODS OUTPUT sets is very likely to be different than from the OUT= options in Proc Phreg so be prepared to seriously modify the code that uses the output. One thing about this ODS OUTPUT is that for procedures that generate ODS graphics you can usually send the information for the plot to a set as well.
The
The SURVEY procs pretty much rely on ODS OUTPUT to create data sets for the various output displayed tables.
If your Proc Surveyphreg is generating the output you need then you would add ODS OUTPUT tablename=desired_dataset; as part of the procedure code. If you don't know the names of the tables to reference you can look at the online help for the procedure in the DETAILS tab where there is a section on ODS Table Names and the options that create the tables.
OR you can add:
Ods trace on; <your procedure code goes here> ods trace off;
The LOG will then show the names of the tables used so you can add the ODS OUTPUT.
This is an example of what the LOG contents from ODS TRACE looks like from Proc Univariate code used in the ODS TRACE documentation:
Output Added: ------------- Name: Moments Label: Moments Template: base.univariate.Moments Path: Univariate.CityPop_90.Moments Label Path: "The Univariate Procedure"."CityPop_90"."Moments" ------------- Output Added: ------------- Name: BasicMeasures Label: Basic Measures of Location and Variability Template: base.univariate.Measures Path: Univariate.CityPop_90.BasicMeasures Label Path: "The Univariate Procedure"."CityPop_90"."Basic Measures of Location and Variability"
The bit you need to create a data set is the Name above. So direct the results of the Moments table to a data set I would add the proc univariate code some like this to create Work.Mymomentdataset.
ods output moments=work.mymomentsdataset;
I suggest doing the bit with ODS TRACE ON / OFF outside of a macro so you know exactly which variables are used and appear in which output sets.
The structure of the ODS OUTPUT sets is very likely to be different than from the OUT= options in Proc Phreg so be prepared to seriously modify the code that uses the output. One thing about this ODS OUTPUT is that for procedures that generate ODS graphics you can usually send the information for the plot to a set as well.
The
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.