* NOTICE: prior to 12/2001 the code posted here had an error in it: a
*  variable "death_dt" (which does not exist) was referred to in the creation
*  of the data set save.colon2; it should be "fu_dt".  Table 8.16 in the
*  book is also incorrect for both the first and second printings.
* ;

libname save 'sasdata';

data temp;
    infile 'data.colon';
    input id 1-4 study 6  rx 8  sex 10 age 12-13  @15 rand_dt mmddyy8.
          obstruct 24  perfor 26  adhere 28  nodes 30-31 
          @33 prog_dt mmddyy8.  prog 42  @44 fu_dt mmddyy8. death 54
          strat1 2. strat2 2.  strat3 2. primary 3. histo 2.
          differ 2. extent 2. implant 2. cea 5.
          +1 sx_dt mmddyy8. +1 trt_dt mmddyy8.;
   
     format rand_dt prog_dt fu_dt sx_dt trt_dt  date7.;

* main analysis data set;
data save.colon;
    set temp;
    if (study=1);   

    keep id study rx sex age surg obstruct perfor adhere nodes differ
	     extent node4 time status etype;

    surg  = 1-strat1;
    node4 = strat2;

    * death;
    time = fu_dt - rand_dt;
    status = death;
    etype=2;
    output;

    * progression;	
    if (prog_dt=.) then prog_dt = fu_dt;
    time = prog_dt - rand_dt;
    status = prog;
    etype=1;
    output;
    
* Secondary anal, under the model of figure 8.15;
data save.colon2;
    set temp;
    if (study=1);

    keep id study rx sex age surg obstruct perfor adhere nodes differ
             extent node4 time status etype;
    surg  = 1-strat1;
    node4 = strat2;

    * progression;
    if (prog_dt=.) then do;
	time = fu_dt - rand_dt;
	status = 0;
	etype='E-R';
	output;

	etype = 'E-D';
	status = death;
	output;
	end;

    else do;
	time = prog_dt - rand_dt;
	status= 1;
	etype='E-R';
	output;

	if (fu_dt = prog_dt) then do;
	    etype = 'E-D';
	    status = death;
	    output;
	    end;
	else do;
	    etype = 'E-D';
	    status = 0;
	    output;
	   
	    etype= 'R-D';
	    status = death;
	    time = fu_dt - prog_dt;
	    output;
	    end;
	end;
