***********************************************************************************************;
**  Program Name    :  adae-s092-cr-cut-p3x-saf.sas                                          **;
**  Date Created    :  23Mar2021                                                             **;
**  Programmer Name :  LIL233                                                                **;
**  Purpose         :  Create adae-s092-cr-cut-p3x-saf                                       **;
**  Input data      :  adae                                                                  **;
**  Output data     :  adae-s092-cr-cut-p3x-saf.html                                         **;
***********************************************************************************************;
options mprint mlogic symbolgen mprint symbolgen mlogic nocenter missing=" ";

**Setup the environment**;
%let prot=/Volumes/app/cdars/prod/sites/cdars4/prjC459/nda2_unblinded_esub/bla_esub_adam/saseng/cdisc3_0;
libname datvprot "&prot./data_vai" access=readonly;

%let codename=adae-s092-cr-cut-p3x-saf;
%let outlog=&prot./analysis/esub/logs/&codename..log;
%let outtable=&prot./analysis/esub/output/&codename..html;

proc printto log="&outlog." new;
run;

data g_adsl_dsin;
   set datvprot.adsl;
   where saffl="Y" and phasen in (2, 3, 4) and agetr01>=16 and MULENRFL ne "Y" 
      and HIVFL ne "Y" and strip(ARM)="Placebo" and VAX201DT>.  and (VAX201DT>. and 
      X1CSRDT>.);
run;

data g_a_dsin;
   set datvprot.adae;
   analysis_subset='Y';
   where AECAT='ADVERSE EVENT' and saffl="Y" and agetr01>=16 and PHASEN in (2, 3, 
      4) and (vphasen >=5 and vphasen ne 99) and .<VAX201DT<=ASTDT<=X1CSRDT and 
      MULENRFL ne "Y" and HIVFL ne "Y" and strip(ARM)="Placebo" and VAX201DT>.;
run;

data g_adsl_dsin;
   set g_adsl_dsin;

   if trt02aN in (8) then
      do;
         newtrtn=1;
         newtrt=coalescec("BNT162b2 (30 (*ESC*){unicode 03BC}g)", trt02a);
         output;
      end;

   if trt02aN in (9) then
      do;
         newtrtn=2;
         newtrt=coalescec("Placebo", trt02a);
         output;
      end;
run;

data g_a_dsin;
   set g_a_dsin;

   if trt02aN in (8) then
      do;
         newtrtn=1;
         newtrt=coalescec("BNT162b2 (30 (*ESC*){unicode 03BC}g)", trt02a);
         output;
      end;

   if trt02aN in (9) then
      do;
         newtrtn=2;
         newtrt=coalescec("Placebo", trt02a);
         output;
      end;
run;

proc format;
   value catlbl 1="Any event" 2="Any serious adverse event" 3="Severe" 
      4="Related(*ESC*){super f}" 5="Life-threatening" 
      6="Any adverse event leading to withdrawal" 7="Death";
   value scatlbl 101, 102, 106="Related(*ESC*){super f}" 201, 202, 206="Severe" 
      301, 302, 306="Life-threatening";
run;

proc sort data=g_adsl_dsin out=_ds1;
   by usubjid newtrtn;
run;

proc sort data=g_a_dsin out=_ds2;
   by usubjid newtrtn;
run;

data final;
   merge _ds1(in=d1) _ds2(in=d2);
   by usubjid newtrtn;

   if d1;

proc sort;
   by newtrtn usubjid;
run;

data final_;
   set final;
   by newtrtn usubjid;
   _uniqid=_n_;
run;

data _basetemplate(compress=no);
   length _varname $8 _cvalue $35 _direct $20 _vrlabel $200 _rwlabel 
      _colabel $800 _datatyp $5 _module $8 _pr_lbl $ 200;
   array _c _character_;
   delete;
run;

data _data1;
   set final_;
   where (NEWTRTN is not missing);

proc sort;
   by NEWTRTN USUBJID;
run;

data _data1;
   retain _trt 0;
   length _str $200;
   _datasrt=1;
   set _data1 end=eof;
   by NEWTRTN USUBJID;
   drop _str;
   _str=' ';
   _lastby=1;
   _dummyby=0;

   if first.NEWTRTN then
      do;

         if not missing(NEWTRTN) then
            do;
               _trt=_trt + 1;
            end;
         _str=NEWTRT;

         if _trt > 0 then
            call symput('_trtlb'||compress(put(_trt, 4.)), trim(left(_str)));
      end;
run;

proc sql;
   create table trtbign as select distinct _trt, newtrt, compress(put(count(*), 
      5.) ) as bign, coalesce(sum(FPX1CUT)/(365.25*100), 0) as tenum from (select 
      distinct USUBJID, _trt, newtrt, FPX1CUT from _data1 where NEWTRTN is not 
      missing) group by _trt;
quit;

proc sort data=_data1 out=_bydat1(keep=_datasrt _dummyby) nodupkey;
   by _datasrt;
run;

data _bydat1;
   set _bydat1 end=eof;
   by _datasrt;
   retain _preby 0;
   drop _preby;
   _byvar1=0;

   if eof then
      do;
         call symput("_preby1", compress(put(_byvar1, 4.)));

         if 0=0 then
            output;
      end;
run;

data _bydat1;
   set _bydat1;
   by _datasrt;
   length _bycol _byindnt $50 _bylast $10;
   _bycol=" ";
   _byindnt=" ";
   _bylast=" ";

proc sort;
   by _datasrt;
run;

proc sort data=_data1;
   by _trt usubjid aeterm descending ATOXGRN;
run;

data tab1;
   set _data1;
   where analysis_subset='Y' and aeterm ne '';
   by _trt usubjid aeterm descending ATOXGRN;

   if last.usubjid then
      do;
         catvar=1;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=1;
         output;
      end;
run;

data rel_tab1;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and aeterm ne '' and upcase(AREL)='RELATED';

   if last.usubjid then
      do;
         catvar=1+100;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=1+100;
         output;
      end;
run;

data sev_tab1;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and aeterm ne '' and ATOXGRN=3;

   if last.usubjid then
      do;
         catvar=1+200;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=1+200;
         output;
      end;
run;

data lif_tab1;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and aeterm ne '' and (atoxgr="GRADE 4");

   if last.usubjid then
      do;
         catvar=1+300;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=1+300;
         output;
      end;
run;

data tab2;
   set _data1;
   where analysis_subset='Y' and aeser='Y';
   by _trt usubjid aeterm descending ATOXGRN;

   if last.usubjid then
      do;
         catvar=2;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=2;
         output;
      end;
run;

data rel_tab2;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and aeser='Y' and upcase(AREL)='RELATED';

   if last.usubjid then
      do;
         catvar=2+100;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=2+100;
         output;
      end;
run;

data sev_tab2;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and aeser='Y' and ATOXGRN=3;

   if last.usubjid then
      do;
         catvar=2+200;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=2+200;
         output;
      end;
run;

data lif_tab2;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and aeser='Y' and (atoxgr="GRADE 4");

   if last.usubjid then
      do;
         catvar=2+300;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=2+300;
         output;
      end;
run;

data tab6;
   set _data1;
   where analysis_subset='Y' and (upcase(aeacn)='DRUG WITHDRAWN' or aesubjdc='Y');
   by _trt usubjid aeterm descending ATOXGRN;

   if last.usubjid then
      do;
         catvar=6;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=6;
         output;
      end;
run;

data rel_tab6;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and (upcase(aeacn)='DRUG WITHDRAWN' or aesubjdc='Y') 
      and upcase(AREL)='RELATED';

   if last.usubjid then
      do;
         catvar=6+100;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=6+100;
         output;
      end;
run;

data sev_tab6;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and (upcase(aeacn)='DRUG WITHDRAWN' or aesubjdc='Y') 
      and ATOXGRN=3;

   if last.usubjid then
      do;
         catvar=6+200;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=6+200;
         output;
      end;
run;

data lif_tab6;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and (upcase(aeacn)='DRUG WITHDRAWN' or aesubjdc='Y') 
      and (atoxgr="GRADE 4");

   if last.usubjid then
      do;
         catvar=6+300;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=6+300;
         output;
      end;
run;

data tab7;
   set _data1;
   where analysis_subset='Y' and (upcase(AEOUT)="FATAL");
   by _trt usubjid aeterm descending ATOXGRN;

   if last.usubjid then
      do;
         catvar=7;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=7;
         output;
      end;
run;

data rel_tab7;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and (upcase(AEOUT)="FATAL") and 
      upcase(AREL)='RELATED';

   if last.usubjid then
      do;
         catvar=7+100;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=7+100;
         output;
      end;
run;

data sev_tab7;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and (upcase(AEOUT)="FATAL") and ATOXGRN=3;

   if last.usubjid then
      do;
         catvar=7+200;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=7+200;
         output;
      end;
run;

data lif_tab7;
   set _data1;
   by _trt usubjid aeterm descending ATOXGRN;
   where analysis_subset='Y' and (upcase(AEOUT)="FATAL") and (atoxgr="GRADE 4");

   if last.usubjid then
      do;
         catvar=7+300;
         output;
      end;

   if last.ATOXGRN then
      do;
         _catvar=7+300;
         output;
      end;
run;

data _data1;
   set _data1(in=a) tab1 sev_tab1 rel_tab1 lif_tab1 tab2 sev_tab2 rel_tab2 
      lif_tab2 tab6 sev_tab6 rel_tab6 lif_tab6 tab7;

   if a then
      do;
         catvar=0;
         _catvar=0;
      end;

   if _catvar ne . then
      catvar=.;
run;

proc format cntlout=tmpfmt;
   select catlbl;
run;

data _anal1;
   length CATVAR 8;
   set _data1;
   where same and CATVAR is not missing;
   _blcksrt=0;
   _cnt=1;
   _cat=1;

   if _trt <=0 then
      delete;
   output;
run;

proc sort data=_anal1;
   by _datasrt _blcksrt CATVAR _trt _cat;
run;

proc sort data=_anal1 out=_temp91 nodupkey;
   by _datasrt _blcksrt _cat CATVAR _trt USUBJID;
run;

proc freq data=_temp91;
   format CATVAR;
   tables _datasrt*_blcksrt*_cat * CATVAR * _trt / sparse norow nocol nopercent 
      out=_pct1(drop=percent);
run;

/*    proc sort data =_anal1 out = _denom1_1(keep=_datasrt _cat) nodupkey;  */
/*    by _datasrt _cat;  */
/*    run;  */
/*  */
/*    data _denom1_1;  */
/*    set _denom1_1;  */
/*    by _datasrt _cat;  */
/*    label count = 'count';  */
/*    _trt = 1;  */
/*    output;  */
/*    _trt = 2;  */
/*    output;  */
/*    run;  */
/*     */
/* proc sql; */
/* create table  _denom1 as select a.*, b.bign as count from _denom1_1 as a left join trtbign as b on a._trt=b._trt; */
/* quit; */
/*     */
data rep1;
   set _pct1;
   length _rwlabel $200. _cvalue $50.;

   if catvar<100 then
      do;
         _rwlabel=strip(put(CATVAR, catlbl.));

         /*  grp=1; */
      end;
   else
      do;
         _rwlabel=repeat(byte(160), 2)|| strip(put(CATVAR, scatlbl.));

         /*    grp=2; */
      end;

   if catvar in (1, 101, 201, 301) then
      _fixvar=1;
   else if catvar in (2, 102, 202, 302) then
      _fixvar=2;
   else if catvar in (6, 106, 206, 306) then
      _fixvar=3;
   else if catvar in (7) then
      _fixvar=4;
   _cvalue=strip(put(count, best.));
run;

proc sql;
   create table rep2 as select a.*, b.bign, b.newtrt, b.tenum from rep1 as a left 
      join trtbign as b on a._trt=b._trt;
quit;

data rep2;
   set rep2;
   length _cpct _cvalue2 $40. CNP_CI $100.;
   newtrt=strip(newtrt)||"| (N(*ESC*){super a}="||strip(bign)||", TE(*ESC*){super b}="||strip(put(tenum, 
      8.1)) || ")";
   _cvalue2=strip(put((count/TENUM), 8.1));

   if count ne 0 then
      lcl=(cinv(0.05/2, 2*count))/(2*TENUM);
   else
      lcl=0;
   ucl=(cinv(1-0.05/2, 2*(count+1)))/(2*TENUM);
   CNP_CI=strip("(" || strip(put(lcl, 8.1)) 
      || ",(*ESC*){nbspace 1}" || strip(put(ucl, 8.1)) || ")");
   _dummy=1;

proc sort;
   by _cat _fixvar catvar _rwlabel _trt newtrt _cvalue;
run;

options topmargin=0.75in bottommargin=0.75in leftmargin=0.75in 
   rightmargin=0.75in;
;
options orientation=LANDSCAPE papersize="LETTER";
ods escapechar="~";
option nobyline;
title1 "Incidence Rates of at Least 1 Adverse Event From Dose 3 to Data Cutoff Date (13MAR2021) (*ESC*){unicode 2013} ";
title2 "Open-Label Follow-up Period (*ESC*){unicode 2013}";
title3 "Subjects Who Originally Received Placebo and Then Received BNT162b2 After Unblinding (*ESC*){unicode 2013}";
title4 "Phase 2/3 Subjects (*ESC*){unicode 2265}16 Years of Age  (*ESC*){unicode 2013} Safety Population";
footnote1 
   "%nrbquote(Note: Dose 3 = First dose of BNT162b2 (30 (*ESC*){unicode 03BC}g).)";
footnote2 
   "%nrbquote(a.~{nbspace 5}N = number of subjects in the specified group.)";
footnote3 "%nrbquote(b.~{nbspace 5}TE = total exposure time in 100 person-years across all subjects in the specified group. Exposure time for a subject is the time from Dose 3 to data cutoff date. This value is the denominator for the incidence rate calculation.)";
footnote4 "%nrbquote(c.~{nbspace 5}n = Number of subjects reporting at least 1 occurrence of the specified event category. For "any 
   event, 
   " n = number of subjects reporting at least 1 occurrence of any event.)";
footnote5 "%nrbquote(d.~{nbspace 5}Incidence rate (IR) is calculated as number of subjects reporting the event/total exposure time in 100 person-years (PY) across all subjects in the specified group.)";
footnote6 "%nrbquote(e.~{nbspace 5}2-sided CI based on Poisson distribution.)";
footnote7 "%nrbquote(f.~{nbspace 5}Assessed by the investigator as related to investigational product.)";
ods html file="&outtable.";

proc report data=rep2 nowd list missing contents="" split="|";
   column _cat _fixvar catvar 
      _rwlabel  ("~S={just=center}Vaccine Group (as Administered)~{line}" newtrt, 
      (_cvalue _cvalue2 cnp_ci _dummy) );
   define _cat / group noprint;
   define _fixvar / group order=internal noprint;
   define catvar / group order=internal noprint;
   define _rwlabel / group "Adverse Event" order=data style(column)={just=left 
      width=65mm} style(header)={just=left} left;
   define newtrt / across nozero "" style(column)={width=35mm leftmargin=12px} 
      style(header)={just=center} center;
   define _cvalue / display nozero "n(*ESC*){super c}" 
      style(column)={leftmargin=12px} style(header)={just=center} center;
   define _cvalue2 / display nozero 
      "IR (/100(*ESC*){nbspace 1}PY)(*ESC*){super d}" 
      style(column)={leftmargin=12px} style(header)={just=center} center;
   define cnp_ci / display nozero "(95% CI(*ESC*){super e})" 
      style(column)={leftmargin=12px} style(header)={just=center} center;
   define _dummy / sum noprint;
   compute before _cat;
      line @1 " ~n";
   endcomp;
   compute after _cat;
      line " ~n";
   endcomp;
run;

ods html close;

proc printto;
run;