Resources

Covariate-Adjusted Survivor Function

 /****************************************************************/
 /*          S A S   S A M P L E   L I B R A R Y                 */
 /*                                                              */
 /*    NAME: PHRSURV                                             */
 /*   TITLE: Covariate-Adjusted Survivor Function                */
 /* PRODUCT: STAT                                                */
 /*  SYSTEM: ALL                                                 */
 /*    KEYS: survival analysis, adjusted survivor function       */
 /*   PROCS: PHREG, SGPLOT                                       */
 /*    DATA:                                                     */
 /*                                                              */
 /* SUPPORT: sasycs                   UPDATE:                    */
 /*    MISC:                                                     */
 /*                                                              */
 /****************************************************************/

 /*
    How should one report the survival curves after fitting a
    Cox model? Of course one can get specific curves corresponding
    to covariate profiles from the BASELINE statement. But often
    in an applied setting, one would like to estimate the survival
    curves for various levels of TREATMENT with all other
    covariates treated as fixed. Hosmer-Lemeshow (1999, p. 145)
    suggest an estimator based on the median of the risk scores on
    the remaining covariates. Marubini et al (1995) advocate the
    direct-adjusted estimator of Makuch (1982).

    Hosmer, D.W. and Lemeshow, S. (1999) Applied Survival Analysis:
       Regression Modelling of Time to Event Data. Wiley.

    Makuch, R.W. (1992) "Adjusted survival curve estimation using
       covariates," Journal of Chronic Disease, 35, pp437-443.

    Marubini E. and Valsecchi, M.G. (1995) Analysis Survival Data
       from Clinical Trials and Observational Studies. Wiley

  */

  /*
    The data set in section 1.3 of Hosmer and Lemeshow (1999) is
    used to illustrate how to compute these two covariate-adjusted
    estimates of the survivor function for each of the long and
    short treatment program.
  */

Data UIS;
   input Id Time Censor Treat Age_C Drug NDrgTx_C;
   label Time='Time from Admission to Relapse (Days)'
         Censor='1=Return to Drug 0=Otherwise'
         Treat='Treatment Program: 0=Short 1=Long'
         Age_C='Age - 35'
         Drug='Drug Use: 0=No 1=Yes'
         NDrgTx_C='No of Prior Drug Treatments - 3';
   datalines;
  1     188       1        1         9       1         -2
  2      26       1        1         3       1          5
  3     207       1        1         3       1          0
  4     144       1        0         2       1         -2
  5     551       0        1        -6       0          2
  6      32       1        1         0       1         -2
  7     459       1        1         9       1         31
  8      22       1        1        -3       1         -1
  9     210       1        1        10       1          0
 10     184       1        1         6       1          4
 12     212       1        1         8       1          5
 13      87       1        1        -1       0         -2
 14     598       0        1         2       1         -1
 15     260       1        1        11       1          5
 16     210       1        1         1       1         -2
 17      84       1        1        -3       1          0
 18     196       1        1        -2       1          3
 19      19       1        1        -2       1         -2
 20     441       1        1         6       0         12
 21     449       1        1         2       1          2
 22     659       0        0         3       0         -2
 23      21       1        0        -2       1          5
 24      53       1        0        -1       1         -3
 25     225       1        0         5       1          6
 26     161       1        0        15       1          3
 27      87       1        0         5       1          2
 28      89       1        0        -6       0          0
 29      44       1        0         6       1          4
 30      37       1        0        -3       1         27
 31     523       0        0         9       1          6
 32     226       1        0         6       1          7
 33     259       1        0         7       1         -2
 34     289       1        0         0       1         -2
 35     103       1        0        14       1          2
 36     624       0        0        -2       0          3
 37      68       1        0        -5       0          0
 38      57       1        1         0       1          4
 39      65       1        0        -6       0         -2
 40      79       1        0        -3       0         -2
 41     559       0        0         0       0         -1
 42      79       1        1         4       1         -3
 43      87       1        1         3       1         -1
 44      91       1        1         4       1          3
 45     297       1        1         6       1         -2
 46      45       1        1        -3       1         -3
 47     246       1        1         5       1         -2
 48      37       1        1        -6       1         -3
 49      37       1        1        -2       0         -1
 50     538       0        1        16       1          5
 51     541       0        1        -4       1         -2
 52     184       1        1        12       1         22
 53     122       1        1         0       0         -3
 55     156       1        1         0       0          0
 56     121       1        0        -3       1         -1
 57     231       1        0         8       1          3
 58     111       1        0        18       1          7
 59      38       1        0         6       1          7
 60      15       1        0        -2       0          4
 61      54       1        0         1       1          2
 62     127       1        0        -2       1          2
 63     105       1        0        -2       0         -2
 64      11       1        0        -4       0         -1
 65     153       1        0         4       1          3
 66      11       1        0        -4       1          2
 67      79       1        0         2       1          3
 68      46       1        0         1       1          2
 69     655       0        0        11       1         -3
 70     166       1        0         0       1         -3
 72      95       1        0         9       1          2
 73      83       1        0        13       1          9
 74     151       1        0         3       1          9
 75     220       1        0         3       1          3
 76     227       1        0        -1       0         -3
 77     343       1        0         6       1          2
 78     119       1        0        -4       1          0
 79      43       1        0         7       1         10
 80     545       0        0        12       1          5
 81      47       1        0        -1       1          5
 82      15       1        0         4       1          0
 83     805       0        0         0       0         -3
 84     321       1        0         5       1         -1
 85     167       1        0         3       0         -2
 86     107       1        0         0       1          1
 87     491       1        0         6       1         -2
 88      35       1        0        -2       1         -1
 89     123       1        0         1       1          9
 90     597       0        0        -7       1         -1
 91     762       0        0         3       1         -2
 92      13       1        0        -4       1          2
 93      31       1        0         7       1          1
 94     228       1        1        -5       0         -2
 95     553       0        1        26       1          6
 96     190       1        1        -7       1         -2
 97     307       1        1        -4       0         -2
 98      73       1        1        -4       1         -2
 99     208       1        1        -7       0         -2
100     267       1        1        -2       1         -1
102     169       1        1         0       1         12
103     125       1        1         2       0         -1
104     655       0        1        -5       1          2
105      70       1        1         3       1          2
106     398       1        1        -8       0          0
107      59       1        1        -2       1         -2
108     122       1        1        -5       1          5
111      96       1        0         8       0         -2
112    1172       0        1         5       0          0
113     734       0        0         5       0         -2
114      26       1        0        -5       1         -2
115      84       1        0         3       0          0
116     171       1        0         0       0         -1
117     159       1        0        15       1         -2
119       7       1        0        12       1         17
120     763       0        0        -1       0         -2
121     104       1        0        -6       0         -3
122     162       1        0         3       1          1
123      90       1        0         2       0         -3
124     373       1        0        -4       0         -3
125     115       1        0        -2       1          0
127      30       1        0         7       1         -2
128       8       1        0        -7       0          3
129     168       1        0        10       1          0
130      70       1        0         6       1         -3
131     130       1        0        -7       1         -1
132     285       1        0         5       0         -2
133     569       0        1        -5       1         -2
134      87       1        1         5       1          3
135     310       1        1        16       1         -3
136      87       1        1         2       0          0
137     544       0        1         5       0         13
138     156       1        1         4       1         -2
139     658       0        1        13       0          0
140     273       1        1         9       1         12
141     168       1        1        -3       1         -1
142      83       1        1         8       1         -2
143       4       1        1         7       1          4
144     708       0        1        14       1          1
145     137       1        1        -5       0         -2
146     259       1        1         4       0         -2
147     560       0        1         4       1         -1
148     586       0        1         8       1         -2
149     190       1        1        -6       1         -1
151     544       0        1        12       1          0
153     494       1        0         4       1         -2
154     541       0        0        15       1          5
155      94       1        0        10       1          1
156     567       0        0        -3       0          0
157      55       1        0        -6       0          0
158      93       1        0        -3       1          0
159     276       1        0         4       0          1
160      46       1        0         6       1          3
162     250       1        0         1       0         -2
163     106       1        0        10       1         -1
164     552       0        0        10       1          1
165      90       1        0         7       1          2
166     203       1        0        -5       1          3
167      67       1        1        11       1          0
168     559       1        1        -8       0         -2
169     106       1        1         1       1          5
170     374       1        1        -1       0         -2
171     630       0        1        -3       0         -1
172      61       1        1        -8       1          0
174     547       0        1         7       1          2
175     568       0        1         6       0         -1
176     490       1        1        -6       0         -2
177     222       1        1        -2       0         -3
178      56       1        1        -6       0         -2
179     282       1        1        -2       0         -1
180      35       1        1        -6       0         17
181     603       0        1        -6       0         -3
183     148       1        1        -1       0         -2
184     354       1        1         7       1          2
185     164       1        1        -4       0         -1
186      94       1        1        -1       1         -2
187      65       1        1        -1       1          1
188     567       0        1         3       0         -2
189     634       0        1        -1       1         -1
190     633       0        1         2       0         -1
191     127       1        1         2       0          1
192     477       1        1         3       0          5
193     436       1        1        -4       1         -1
195     362       1        1        -6       1          1
196     552       0        1        16       1         -1
197     144       1        1        -7       0         -1
198     242       1        1        10       1          5
199     564       0        1        18       0          1
200     299       1        1         3       0         -3
201     167       1        1        -9       0          4
202     380       1        1         8       0         -1
203     120       1        1        -2       1          1
204     248       1        0         0       1          3
205     218       1        1         9       1          3
206     115       1        0         7       1         -2
207     224       1        0         2       0          1
208     132       1        0         9       1         -1
209     148       1        0        -2       1          7
210     593       0        0        -4       0         -3
211      26       1        0         1       1         -3
213      32       1        0         4       1          5
214     292       1        0        -4       1         -1
215      89       1        0        -1       0          0
216      21       1        0         3       1          4
217     364       1        0        -5       0          5
218     142       1        0         4       1          0
219     188       1        0         2       0         -1
221      92       1        0         8       1         -3
222      56       1        0         2       1          5
223     110       1        0         1       1          4
224     555       0        0        10       0          0
225     220       1        0        -2       0          2
226      23       1        0        10       1          0
227     285       1        0         2       1          0
228      90       1        0        -1       1          2
229      59       1        0        -5       0          1
230     156       1        0         2       1         -1
231     194       1        0         6       1         -1
232     142       1        0         7       1         -1
233      57       1        0         8       1          5
234     279       1        0         1       1          6
235     118       1        0         0       1          0
236     567       0        0         4       0          1
237     562       0        1         3       1          1
238     239       1        1         6       1          7
239     578       0        1         2       0         -3
240     551       0        1        -1       1         -1
241     313       1        1         2       0          1
242     560       0        1         4       0          4
243      54       1        1        -4       1         28
244     198       1        1        -5       1          2
245     164       1        1         0       0         -1
246     325       1        1         3       1          3
247      62       1        1        10       1          3
248      45       1        1        -4       0          9
249      53       1        1        -4       1          2
250     253       1        1         5       1          1
251      51       1        1        -4       1          8
252     540       0        1         3       0          0
253     317       1        1        -3       1          1
254     437       1        1        -1       1          0
255     136       1        1        -1       1          6
256     115       1        1         9       1          4
257     175       1        1        13       1         -1
258     442       1        1         5       1          0
259     122       1        1        -4       0         -1
260     181       1        1        -3       0          0
261     180       1        1        -2       0         -1
262      51       1        1         0       0          1
263     541       0        1         1       1         -2
264     121       1        1        -4       1         13
265     328       1        1         6       1          1
267     166       1        1        -5       1          0
268     556       0        1        -4       0         -3
269     104       1        0         0       1          2
270     102       1        0        -2       0          2
272     144       1        0         4       0         -3
273     545       0        0         1       0         -1
274     537       0        0         0       1         -2
275     625       0        0         5       1          2
276       6       1        0         0       1          0
277     307       1        0         7       1          4
278     290       1        0        -1       0          9
279      20       1        0        -7       0          5
280      74       1        0        -7       0          1
281     100       1        0        14       0         -3
282     555       0        0        13       1          5
283     152       1        0         8       1          0
284     115       1        0         3       0          0
285      92       1        0         6       1          6
286     554       0        0        -4       1         -1
287      92       1        0         0       0         -3
288      69       1        0         4       0         -3
289      25       1        0         2       1          3
290     501       0        0         0       0         -2
291      86       1        0         7       1          7
292      99       1        0         5       1          4
293      87       1        0         0       0         -3
294     136       1        0         4       0          3
295     106       1        0        10       1          3
296     220       1        0        -2       1          0
297      36       1        0         2       0          3
298     162       1        0        15       1         -1
299     116       1        1        -6       0         -3
300     175       1        1        13       1          2
301     209       1        1         8       1         -3
302     545       0        1         3       1         -2
303     245       1        1        -4       1         -1
304     176       1        1        10       1          0
305      14       1        1        -3       1         -3
306     113       1        1         9       1          9
307     159       1        1        -1       0         -1
308     354       1        1        -1       1          0
309     174       1        1        -2       1          1
310      23       1        0         7       1         17
311      26       1        0         7       1         17
312      98       1        0        10       1          6
313      23       1        0        -5       1          2
314     555       0        0        10       0         -1
315     290       1        0        10       1          1
316     543       0        0         4       1          5
317     274       1        0        11       1          4
321     119       1        1        -7       0         -2
322     164       1        1         6       0          0
323     548       0        1        -2       0         -1
324     175       1        1        -7       0          0
325     539       0        1        -3       0          0
326     155       1        1         2       1         -3
327      14       1        1         8       1         12
328     187       1        1         8       0          3
329      65       1        1        15       1          5
330     159       1        1        -4       1         -2
331      96       1        1        -1       1         32
332     243       1        1         3       0          0
333      85       1        1        -5       0         -3
334       4       1        1         6       0         -1
335     121       1        1         7       1          1
336     659       1        1        -1       0         -3
337     260       1        1        -1       0          0
338     621       0        1         5       1         -2
339     199       1        1         3       0         -2
340     565       0        1         1       1         -1
341     183       1        1         7       1          2
342     122       1        1         2       1          1
343     170       1        1         3       1         10
344      15       1        1         6       0         11
345     268       1        1        -4       0          2
346      79       1        1         5       0         -3
347      23       1        0        -5       1          3
348     100       1        0         3       1         27
349      98       1        0         6       1          5
350      81       1        0         8       1          3
351     546       0        0         6       1          0
352      58       1        0         6       1          7
353     569       0        0         5       1          3
354     575       0        0         5       0          0
356      91       1        0         9       1          1
357      57       1        0         7       1          3
358     499       1        0         0       1          2
359     123       1        0        -4       1          4
360     143       1        0         9       0          2
362     471       1        0         2       0          1
363      74       1        0        -4       0          1
364      85       1        0         3       1          3
365      95       1        0         6       1          2
366      36       1        1        12       1          0
367      19       1        1         7       1         19
368      38       1        1         7       1         -2
369     539       0        1         2       0          1
370     567       0        1        -7       0          0
371     186       1        1        -9       0          7
372     546       0        1        -7       0         -2
373      24       1        1         4       1         -2
374     540       0        1         3       1          0
375     157       1        1         3       0          1
376      86       1        1        -4       1         -3
377     231       1        1        -2       0         -1
379      14       1        0        -3       1          0
380      75       1        0        -8       1         -1
381     147       1        0         1       1          3
382     105       1        0        -1       0          1
383     324       1        0        14       1          9
384     538       0        0        -4       0         -3
385     300       1        0        14       1         13
386      73       1        0         4       1         -2
387      65       1        0         6       1          3
388     568       1        0        13       1         17
389      84       1        0         7       0         -2
390      22       1        1        14       1         14
391      44       1        1         1       1         27
392       7       1        1        -6       0          0
394      21       1        1         7       1          1
395     537       0        1        11       1          9
396     186       1        1         2       0          0
397      40       1        1        -7       0         -1
398     287       1        1         3       1          0
399     538       0        1        13       0          6
400      30       1        1         3       1          5
401     516       1        1        11       1          0
402     268       1        1        11       1         -1
403     568       0        1         7       1          0
404     131       1        0        -4       0          0
405     399       1        0         3       1          3
406      78       1        0         0       0         -1
407      80       1        0         3       0          3
408     102       1        0         3       1          0
410     124       1        0         7       0          3
411      80       1        0        14       1         17
412      23       1        0       -10       0         -2
413     274       1        0         3       1          1
415     459       1        0         6       1          0
416      10       1        0         4       1          0
417     176       1        1         5       1         14
418     332       1        1         8       1         23
419     119       1        1        -6       0          0
420     217       1        1        -6       0          1
421     285       1        1         2       0          1
422     576       0        1         5       1          0
423     106       1        1         3       1          2
424      81       1        1        -1       1          1
425      47       1        1        12       1         27
426      76       1        1        -7       0         -2
427     348       1        1        -2       1         13
428      20       1        1         9       0          2
429     306       1        1        13       1          1
430     192       1        0         3       0         -3
431     216       1        0         7       1         17
432     189       1        0        19       1          4
434     193       1        0         6       0         -2
435      28       1        0        -3       1         10
436     150       1        0         5       1         -2
437      99       1        0        -5       1          0
438     510       0        0         3       1          0
439     306       1        0         4       1          4
440     101       1        0         8       1          7
441     102       1        0         6       1         -3
442     510       0        0        -3       1         -2
444     503       0        0        10       1         -3
445      52       1        0         2       1          0
446     547       0        0         8       1         -2
447     168       1        0         8       1          3
448     461       1        0        -7       0         -3
449     538       0        0        -4       1         -1
450     349       1        0         6       1          4
451      44       1        0         0       0          2
452     548       0        1         1       1          0
453      12       1        1        -7       0         -2
454       6       1        1        13       1         -3
455     575       0        1        -6       0         -2
456     589       0        1        12       0         -2
457     408       1        1         5       1         -2
458     232       1        1        -9       0         -3
459     143       1        1        15       1         -3
460     582       0        1        13       1          3
461     134       1        1        -6       0         -3
462       7       1        1         7       1         -2
463     548       0        1        10       1         -3
464      81       1        0        -3       1         -1
465     170       1        0        -1       1         -2
466      29       1        0         4       1          3
467      78       1        0        -1       1         17
468      81       1        0         5       1          2
469     369       1        1         9       1          1
470      69       1        1        11       1         -3
471     115       1        1         7       0          3
472     361       1        1         0       1          4
473     245       1        1         1       0         -3
474     233       1        0        10       1          5
475     227       1        0         2       0         -3
476      97       1        0         2       1          0
477     547       0        0        -3       0         -3
478     224       1        0        -7       0         -3
479     211       1        0        -7       0         -1
480     220       1        0        13       1          9
481      54       1        0        -6       0         -3
482     192       1        0         6       0         -1
483     138       1        0        -1       1         -2
484     107       1        1         9       1         -2
485     597       0        1        -7       0         -3
486     226       1        1        -3       1          1
487     434       1        1        -4       1          2
488     106       1        1        -4       0         -2
489     180       1        1        -5       0         -3
490     557       0        1         4       0         -3
491     556       0        1        15       1         -1
492     619       0        0        -7       0         -2
493     546       0        0        -4       0         -3
495     233       1        0        -6       1         -1
496     102       1        0         0       1         -1
497     548       0        0        -8       0         -3
498      99       1        0        -5       0         -2
499      36       1        0         0       1         37
501      78       1        1         3       0         -3
502     502       1        1         4       1          5
503      71       1        1        -1       0         -1
504      59       1        1         9       1          3
505     115       1        1         3       0         -2
506     533       0        1        -4       0          1
507      10       1        1         2       1          3
508     274       1        0        12       1         -1
509     255       1        0         9       1          5
510     503       0        0         6       0         -3
511     256       1        0        -8       0         -2
512       9       1        0        -3       0         -2
514     386       1        0        -2       0         -3
515     547       0        1         6       1         -2
516      45       1        1         1       0          0
517      58       1        1        -3       1          1
518     124       1        1        -7       0         -2
519     540       0        0        -6       1         17
520     243       1        0         8       1         -2
521     549       0        1        -5       0         -2
522      12       1        0        -4       0         -3
523      51       1        0        -8       0         -1
524     562       0        1         3       0         -1
525      94       1        1         0       1          4
526     204       1        0        15       1          0
527     238       1        0        -6       1         -3
528     140       1        1        -3       0         -3
529     120       1        1         6       1          7
530     154       1        0         8       1         -1
531     177       1        1         1       1         -1
532     119       1        1        10       1          4
533      83       1        1        -8       0         -3
534     130       1        1         1       0         -3
535      11       1        1        -7       1         17
536     159       1        0         9       1          0
537     211       1        0        -7       0         -2
538      33       1        0         3       0         -2
539      72       1        1        -3       1         -1
540     161       1        1         7       0         -1
541     191       1        0         5       1         22
542     181       1        0        -5       0          2
543     546       0        1        -3       0         -2
544     540       0        0         4       0         -3
545      76       1        0         0       0          0
546       7       1        0         5       1          4
547      44       1        0         4       0         -3
548     103       1        0        -5       1          2
549      79       1        0         4       0         -2
550     339       1        0        -6       1          0
551      90       1        0        -6       0         -1
552     542       0        0        10       0          0
553     384       1        0         3       0         -2
554     255       1        1         8       1         -2
555     431       1        1         2       0         -3
556     587       0        0         3       0         -2
557     198       1        0        -2       0         -1
558     551       0        0         1       1         -1
559     110       1        1         1       1          1
560     541       0        1         3       0         -3
561     242       1        0        -6       0          8
562     537       0        0        -4       0          0
563      56       1        1         1       1          4
564      34       1        1        10       1          1
566     549       0        1         4       0          0
567     133       1        0         0       1          4
568     226       1        0         6       1          0
569     401       1        1         8       1          3
570      14       1        1         1       1          3
571     548       0        0        -3       1         -1
572     224       1        1         2       1         12
573     540       0        0         5       0          2
574     237       1        0        14       1         10
575     354       1        1         1       1          7
576     123       1        0        -2       1         17
577     170       1        0        10       1         -2
578     203       1        1        -5       0          7
579     360       1        1         2       1          3
580     139       1        0        -1       0         -1
581     215       1        1        10       1          2
582     129       1        1        -3       1         -3
583     396       1        1        -4       0         -2
584     547       0        1         4       0          1
585     547       0        0        19       1         -1
586      71       1        1        -9       1         -2
587     168       1        1         9       1         -1
588     228       1        1         3       1          1
589     551       0        1         2       0         -2
590     654       0        0         5       1          3
591      51       1        1         1       0         -3
592     548       0        0        -2       0          0
593     231       1        1        -3       1          2
594     280       1        1        -1       0         -2
595     184       1        0         0       0         17
596      86       1        0        -3       0          0
598      46       1        1        10       1         -2
599     200       1        1         7       0         -1
600     244       1        0         3       0         -3
601     182       1        0        -2       0         -1
602     296       1        1        10       1          5
603      24       1        0        18       0         -3
604     142       1        0         8       0         -2
605     120       1        0         5       0         -3
606      47       1        0         7       1         -3
607     519       1        1        -5       0         -3
608     248       1        1        -4       0         -1
609      31       1        0         0       1          0
610     567       0        0        -2       1         -1
611     353       1        0        -7       1          1
612     458       1        1         6       0         -2
613     554       0        1         6       1          1
614     116       1        0        -6       0         -3
615      74       1        0         3       1          2
616      10       1        0         8       1          3
617     355       1        1        11       1         18
618     232       1        1         1       0         -3
619      68       1        1         1       1          8
620      48       1        1         7       0         -3
621      60       1        0         7       1          1
622      50       1        0         3       0         -3
624     126       1        0        23       1          3
625      18       1        0         7       1          1
626      35       1        1        -2       1          0
627     379       1        0         5       1         -1
628     377       1        1        16       1         12
;


 *In the following PROC PHREG run, three SAS data sets are created:
 1) Est1 contains the regression coefficient for the variable Treat
 2) Outp contains the variable Treat and risk-score variable xbeta
    for each input observations
 3) Outb contains the estimated survival at the mean;
proc phreg data=uis outest=Est1(keep=TREAT);
   model TIME*CENSOR(0)=TREAT AGE_C DRUG NDRGTX_C;
   output out=Outp(keep=TREAT xbeta)xbeta=xbeta ;
   baseline out=outb xbeta=xbeta survival=survival;
   run;


 *-----------------------------------------------------------------
  Hosmer and Lemeshow (1999, p.145) suggest subtracting out the
  contribution of the grouping variable from the risk score,
  calculating the median value of what remains, and then adding
  back in the contribution of the grouping variable when
  calculating the estimator of the survival function.
  ----------------------------------------------------------------;
  *Compute the risk scores for the remaining covariates;
data out1;
   if _n_=1 then set est1(rename=(TREAT=b1));
   set outp;
   xbeta= xbeta - TREAT*b1;
   run;


 *Compute the median of the risk scores;
proc means data=Out1 noprint;
   var xbeta;
   output out=rm50(keep=rm50) median=rm50;
   run;

 *Compute the risk score-adjusted survival for Treat=0 and treat=1;
data Hosmer(keep=Group Time Survival);
   if _n_=1 then do; set rm50; set est1(rename=(TREAT=b1)); end;
   set outb(in=in0) outb(in=in1);
   if in0 then do;
      Survival= Survival ** exp(rm50 - xbeta);
      Group='Treat=0';
   end;
   else do;
      Survival= Survival ** exp(rm50 + b1 - xbeta);
      Group='Treat=1';
   end;
 run;

 *Plotting the risk score-adjusted survival curves;
title 'Risk Score-Adjusted Survival Curves';
proc sgplot data=Hosmer;
   yaxis values=(0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0)
         label="Survival Probability";
   step y=Survival x=Time / group=Group
                            name="Survival";
   keylegend "Survival" / location=outside;
   run;


 *-----------------------------------------------------------------
  Maurbini and Valsecchi (1995, Chapter 6) advocates the direct-
  adjusted survival curve of Makuch (1982), which is obtained by
  averaging the individual estimated survivor curves over all
  subjects at each survival time.
  ----------------------------------------------------------------;
data Out0(keep=xbeta) Out1(keep=xbeta);
   set Outp;
   if Treat=0 then output Out0;
   else output Out1;
   run;

 *Average the individual survival curves in Treat=0;
data s0(keep=Time s);
   retain s 0;
   set outb(rename=(xbeta=xb)) end=eof;
   do i=1 to n;
      set Out0 point=i nobs=n;
      s= s + Survival ** exp(xbeta-xb);
      if i=n then s=s/n;
   end;
   run;

 *Average the individual survival curves in Treat=1;
data s1(keep=Time s);
   retain s 0;
   set outb(rename=(xbeta=xb)) end=eof;
   do i=1 to n;
      set out1 point=i nobs=n;
      s= s + Survival ** exp(xbeta-xb);
      if i=n then s=s/n;
   end;
   run;
 *Concatenate the two direct-adjusted survival curves in one
  data set;
data Direct(rename=(s=Survival));
   set s0 (in=in0) s1;
   if in0 then Group='Treat=0';
   else Group='Treat=1';
   run;

*Plotting the direct-adjusted survival curves;
title 'Direct Covariate-Adjusted Survival Curves';
proc sgplot data=Direct;
   yaxis values=(0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0)
         label="Survival Probability";
   step y=Survival x=Time / group=Group
                            name="Survival";
   keylegend "Survival" / location=outside;
   run;


 *-----------------------------------------------------------------
   Plotting the risk score-adjusted survival curves and the direct-
   adjusted survival curve on the same graph.
  ----------------------------------------------------------------;
data both;
   length Group $ 25;
   set Hosmer(in=HL) Direct;
   if HL then do;
      if Group='Treat=0' then Group='Risk-Adjusted: Treat=0';
      else                    Group='Risk-Adjusted: Treat=1';
   end;
   else do;
      if Group='Treat=0' then Group='Direct-Adjusted: Treat=0';
      else                    Group='Direct-Adjusted: Treat=1';
   end;
run;

title 'Covariate-Adjusted Survival Curves';
proc sgplot data=Both;
   yaxis values=(0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0)
         label="Survival Probability";
   step y=Survival x=Time / group=Group
                            name="Survival";
   keylegend "Survival" / location=outside;
   run;