## Plotting the log hazard/survival vs a variable

Solved
Occasional Contributor
Posts: 8

# Plotting the log hazard/survival vs a variable

[ Edited ]

Hi,

I'm using survival data and I want to plot the log hazard of the outcome vs age (a variable in my data set) to see if the relationship between age and the log hazard is linear, but I'm not sure how to create such a plot.

For example, how would make the plot using this data set:

data kidney;
input time cens age nephrectomy @@;
cards;
9 1 50 0 6 1 55 0 21 1 45 0 15 1 52 0 8 1 62 0 17 1 62 0 12 1 53 0 104 0 61 1
9 1 41 1 56 1 51 1 35 1 61 1 52 1 41 1 68 1 31 1 77 0 71 1 84 1 71 1 8 1 51 1
36 1 61 1 72 1 51 1 36 1 41 1 48 1 31 1 26 1 41 1 108 1 31 1 5 1 41 1 108 0 52 1
26 1 52 1 14 1 62 1 115 1 52 1 52 1 42 1 5 0 25 1 18 1 42 1 36 1 52 1 9 1 62 1
10 1 43 1 9 1 53 1 18 1 43 1 6 1 43 1
;
run; proc print;

Accepted Solutions
Solution
‎10-02-2017 07:09 PM
Posts: 1,138

## Re: Log hazards

Consider this SAS example using PROC LIFETEST: Example 70.3 Life-Table Estimates for Males with Angina Pectoris, which includes "and estimated hazard function are requested by the PLOTS= option".

All Replies
Super User
Posts: 21,444

## Re: Plotting the log hazard/survival vs a variable

Have you checked if this functionality is in PROC PHREG already? What version of SAS do you have?

What does your PROC PHREG code look like, ie how are you calculating hazard and are there other variables involved?

Hi,

I'm using survival data and I want to plot the log hazard of the outcome vs age (a variable in my data set) to see if the relationship between age and the log hazard is linear, but I'm not sure how to create such a plot.

For example, how would make the plot using this data set:

data kidney;
input time cens age nephrectomy @@;
cards;
9 1 50 0 6 1 55 0 21 1 45 0 15 1 52 0 8 1 62 0 17 1 62 0 12 1 53 0 104 0 61 1
9 1 41 1 56 1 51 1 35 1 61 1 52 1 41 1 68 1 31 1 77 0 71 1 84 1 71 1 8 1 51 1
36 1 61 1 72 1 51 1 36 1 41 1 48 1 31 1 26 1 41 1 108 1 31 1 5 1 41 1 108 0 52 1
26 1 52 1 14 1 62 1 115 1 52 1 52 1 42 1 5 0 25 1 18 1 42 1 36 1 52 1 9 1 62 1
10 1 43 1 9 1 53 1 18 1 43 1 6 1 43 1
;
run; proc print;

Occasional Contributor
Posts: 8

## Re: Plotting the log hazard/survival vs a variable

[ Edited ]

Hi Reeza,

This is not a real data set. It is a small data set that I have created just practice. I'm currently using SAS studio.

data kidney;
input time cens age nephrectomy @@;
cards;
9 1 50 0 6 1 55 0 21 1 45 0 15 1 52 0 8 1 62 0 17 1 62 0 12 1 53 0 104 0 61 1
9 1 41 1 56 1 51 1 35 1 61 1 52 1 41 1 68 1 31 1 77 0 71 1 84 1 71 1 8 1 51 1
36 1 61 1 72 1 51 1 36 1 41 1 48 1 31 1 26 1 41 1 108 1 31 1 5 1 41 1 108 0 52 1
26 1 52 1 14 1 62 1 115 1 52 1 52 1 42 1 5 0 25 1 18 1 42 1 36 1 52 1 9 1 62 1
10 1 43 1 9 1 53 1 18 1 43 1 6 1 43 1
;
run; proc print;
proc phreg data=kidney plots= survival;
model time*cens(0)= age nephrectomy;
run;

I just want to get a plot of mode time*cens(0)= age

Thank you,

Occasional Contributor
Posts: 8

## Log hazards

Hi,

I'm using survival data and I want to plot the log hazard of the outcome vs age (a variable in my data set) to see if the relationship between age and the log hazard is linear, but I'm not sure how to create such a plot.

For example, how would make the plot using this data set:

data kidney;
input time cens age nephrectomy @@;
cards;
9 1 50 0 6 1 55 0 21 1 45 0 15 1 52 0 8 1 62 0 17 1 62 0 12 1 53 0 104 0 61 1
9 1 41 1 56 1 51 1 35 1 61 1 52 1 41 1 68 1 31 1 77 0 71 1 84 1 71 1 8 1 51 1
36 1 61 1 72 1 51 1 36 1 41 1 48 1 31 1 26 1 41 1 108 1 31 1 5 1 41 1 108 0 52 1
26 1 52 1 14 1 62 1 115 1 52 1 52 1 42 1 5 0 25 1 18 1 42 1 36 1 52 1 9 1 62 1
10 1 43 1 9 1 53 1 18 1 43 1 6 1 43 1
;
run; proc print;

I want to get a plot of model time*cens(0)=age

Solution
‎10-02-2017 07:09 PM
Posts: 1,138

## Re: Log hazards

Consider this SAS example using PROC LIFETEST: Example 70.3 Life-Table Estimates for Males with Angina Pectoris, which includes "and estimated hazard function are requested by the PLOTS= option".

Occasional Contributor
Posts: 8

## Re: Log hazards

thank you!

Occasional Contributor
Posts: 8

## Re: Log hazards

I'm actually trying to create a plot just similar to the one below. I tried Proc Phreg, but it does not create such plot.

SAS Super FREQ
Posts: 3,898

## Re: Log hazards

Did you try using the TRANSFORM=LOG option on the PROC PHREG statement, as described in the doc?

Occasional Contributor
Posts: 8

## Re: Log hazards

Hi,

No, I have not tried that yet, but the proc phreg does not create a hazard rate plot. If I were to use that option, I'm not sure which function I should to transform.

Thanks

Occasional Contributor
Posts: 8

## Re: Log hazards

[ Edited ]

Hi @Rick_SAS,

so if this is my SAS code:

proc phreg data=kidney plots=cumhaz ;
model time*cens(0)=age / rl ;

run;

how do I use the transform statement to take the log of the cumhaz.

SAS Super FREQ
Posts: 3,898

## Re: Log hazards

I was wrong when I said that you could use the TRANSFORM= option in PROC PHREG.

However, you can create an output data set from any table or graph that SAS produces. Please see

Thus the easiest way to accomplish your task is to write the cumulative hazard values to a data set and then simply tell PROC SGPLOT to use a log axis, like this:

``````proc phreg data=kidney plots=cumhaz ;
model time*cens(0)=age / rl ;
ods output CumHazPlot=CHPlot;
run;

title "log10(cumulative hazard) for Age=49.39";
proc sgplot data=CHPlot;
where CumHaz>0;
step y=CumHaz x=Time;
yaxis type=log logbase=10 logstyle=logexpand;
run;``````

Alternatively, you can use the DATA step to log-transform the cumulative hazard, and then use a regular (linear) scale on the axis:

``````data logCumHaz;
set CHPlot;
if cumHaz=0 then logCumHaz=.;
else logCumHaz= log10(cumHaz);
label logCumHaz = "log10(cumulative hazard)";
run;

title "log10(cumulative hazard) for Age=49.39";
proc sgplot data=logCumHaz;
step y=logCumHaz x=Time;
run;``````

Occasional Contributor
Posts: 8

## Re: Log hazards

@Rick_SAS Thank you for your help. This is exactly what I wanted to do, but I want to plot age instead of time on the x-axis. When I change the x-axis to age, I get an error message saying that the variable age is not found. So I'm guessing that the variable age is not found in the new data set "CHPlot". So how do I include age in the new dataset "CHPlot".

Thank you so much for your help, I really appreciate it.

SAS Super FREQ
Posts: 3,898

## Re: Log hazards

In looking back at your original post, I see that you did mention plotting against age, which is a covariate. Thus my idea will not work.I suggest you look at the OUTPUT statement or the STORE statements to get the information that you want. This now sounds like a statistical question rather than a graphing question. I do not use PHREG myself, so I will defer to those with more experience.  Good luck.

Super User
Posts: 21,444