/*****************************************************************/
  /* outcomes:                                                     */
  /*                                                               */
  /* 023.Genital infection - AT, ITT                               */
  /*****************************************************************/

  libname new XLSX "/storage1/PHShome/rl037/rishi_medicare_rpdr/may2022/ci2_codebook.xlsx";

   data outcome_dx(rename=(code_type = type));
      set new.outcome;
   dx = left(compress(code, '.'));
   if dx ^= ' ';
   keep code_type dx;
   run;

   proc sort nodupkey;
     by dx;
   run;

   data  infection09
         infection10;
       set outcome_dx;
   if type ='09' then output infection09;
   if type ='10' then output infection10;
   keep dx;
   run;


 data lastdate_itt(rename=(lastdate = lastdate_itt));
   set out.dm_cohort(rename=(enr_end = enddt));
  end_data = '31Dec2019'd;
  lastdate = min( deathdt, enddt,end_data);
  keep patid indexdt class lastdate deathdt;
  run;


 data lastdate_at(rename=(lastdate= lastdate_at));
   set out.lastdate;
 keep patid indexdt class lastdate;
 run;

 data ids;
     merge lastdate_itt
           lastdate_at;
    by patid;
  keep patid indexdt class lastdate_itt lastdate_at ;
 run;


   %macro hosp;

         %do year = 2012 %to 2019;

       proc sql;
         create table dx&year(keep=patid indexdt admit_date dx enc_type dx_type pdx) as
         select * from in.diagnosis&year(keep= patid admit_date dx enc_type dx_type pdx ) dx,
                  ids
         where ids.patid    = dx.patid     and
               admit_date > indexdt;
      quit;


       proc sort nodup data = dx&year;
          by patid indexdt admit_date dx;
       run;


       %end;


  data all_dx(rename=(dx_type = dx_codetype));
    set  dx2019
          %do year = 2012 %to 2018;
              dx&year
          %end;
          ;
   run;

   %mend;
   %hosp;


   data dx09 dx10;
            set all_dx;
        if dx_codetype = '09' then output dx09;
        if dx_codetype = '10' then output dx10;
        keep patid indexdt admit_date dx ;
        run;
  */
 proc sql;
        create table dx09_outcome(keep= patid indexdt admit_date) as
        select * from  infection09(keep = dx) temp,
                 dx09(keep = patid dx indexdt admit_date) claims
        where temp.dx    = claims.dx ;
     quit;


   proc sort nodup;
     by patid;
  run;


  proc sql;
        create table dx10_outcome(keep= patid indexdt admit_date) as
        select * from  infection10(keep = dx) temp,
                 dx10(keep = patid dx indexdt admit_date) claims
        where temp.dx    = claims.dx ;
     quit;


   proc sort nodup;
     by patid;
  run;

   data outcome(rename=(admit_date = outcome_dt));
        set dx10_outcome
            dx09_outcome;
  keep patid admit_date;
  run;


 proc sort data = outcome;
    by patid outcome_dt;
 run;

 proc sort nodupkey data = outcome;
    by patid;
 run;

 data out.genial_at;
     merge outcome
           ids(in = in1 keep = patid indexdt class lastdate_at);
     by patid;
     if in1;
 outcome_genial = 0;
    if indexdt < outcome_dt <= lastdate_at then outcome_genial = 1;
 keep patid indexdt outcome_dt outcome_genial lastdate_at class;
 run;

  proc freq;
    table outcome_genial;
  run;

 data out.genial_itt;
     merge outcome
           ids(in = in1 keep = patid indexdt class lastdate_itt);
     by patid;
     if in1;
 outcome_genial = 0;
    if indexdt < outcome_dt <= lastdate_itt then outcome_genial = 1;
 keep patid indexdt outcome_dt outcome_genial lastdate_itt class;
 run;

  proc freq;
    table outcome_genial;
  run;