pmbrown

derive_WR.sas

Nov 11th, 2020
55
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /******************************************************************************
  2. Author, date: Paul Brown, SEP2015
  3. Macro name:   derive_WR
  4. Description:  derive the unmatched win-ratio endpoint on simulated data.
  5.               (developed in SAS 9.4)
  6. Reference:    The win ratio: a new approach to the analysis of composite
  7.               endpoints in clinical trials based on clinical priorities,
  8.               Pocock SJ et al., EHJ, 2012
  9. ******************************************************************************/
  10.  
  11. ********************************************************************;
  12. ***             Rank survival times and count ties               ***;
  13. ********************************************************************;
  14.  
  15. %macro surv_wr(indata_,var,where);
  16. proc sort data=&indata_ (where=&where) out=&var;
  17.   by samp &var;
  18. run;
  19.  
  20. data rank&var;
  21.   retain ties&var before&var count 0;
  22.   set &var;
  23.   by samp &var;
  24.   if first.samp then count=0;
  25.   else count=count+1;
  26.   if first.&var then do;
  27.     ties&var=1;
  28.     before&var=count; /*can check using ties=low option in proc rank*/
  29.   end;
  30.   else ties&var=ties&var+1;
  31.   keep samp subjno trt &var ties&var before&var;
  32. run;
  33.  
  34. proc sort data=rank&var out=ties&var (keep=samp &var ties&var);
  35.   by samp &var descending ties&var;
  36. run;
  37.  
  38. proc sort data=ties&var nodupkey;
  39.   by samp &var ;
  40. run;
  41.  
  42. data rank&var.2;
  43.   merge rank&var (drop=ties&var) ties&var;
  44.   by samp &var;
  45. run;
  46.  
  47. proc sort data=rank&var.2;
  48.   by samp subjno;
  49. run;
  50. %mend surv_wr;
  51.  
  52. ********************************************************************;
  53. ***                 Derive win-ratio score (U)                   ***;
  54. ********************************************************************;
  55.  
  56. %macro derive_wr(n=100,indata=finalsamp,outdata=out_wr,outpval=pval_wr);
  57.  
  58. %surv_wr(&indata,var1,(var1c=0));
  59. %surv_wr(&indata,var2,(var1c=1 and var2c=0)); /*consider reh for pts who didnt die*/
  60.  
  61. *consider where pts fail;
  62. data winratio;
  63.   merge &indata rankvar12 rankvar22;
  64.   by samp subjno trt;
  65.   if var1c=0 then cat=1;      /*pt died*/
  66.   else if var2c=0 then cat=2; /*pt didnt die, had rehosp*/
  67.   else cat=3;
  68. run;
  69.  
  70. *find the number of pts falling into these broad categories to enable
  71.  calculation of no. of wins/losses/ties per pt;
  72. ods output onewayfreqs=counts1;
  73. proc freq data=winratio;
  74.   ods select onewayfreqs;
  75.   tables cat ;
  76.   by samp;
  77. run;
  78.  
  79. proc transpose data=counts1 out=counts1_t (drop=_name_) prefix=cat;
  80.   var frequency;
  81.   id cat;
  82.   by samp;
  83. run;
  84.  
  85. *replace missings obs with 0 (when n small have small or missing cell counts);
  86. proc stdize data=counts1_t
  87.             out=counts1_t2 reponly missing=0;
  88. run;
  89.  
  90. *calculate number of wins/losses/ties for each pt, ie as if comparing each
  91.  pt to every other pt (irrespective of trt group) as per the unmatched approach;
  92. data &outdata;
  93.   merge winratio counts1_t2 ;
  94.   by samp;
  95.   if cat=1 then do;      /*compare mortality times*/
  96.     win=beforevar1;
  97.     tie=tiesvar1-1; /*subtract 1 so the pt isnt tied with themselves*/
  98.     lose=(&n-1)-win-tie;
  99.   end;
  100.   else if cat=2 then do; /*cant determine who died 1st, use rehosp*/
  101.     win=cat1+beforevar2;
  102.     tie=tiesvar2-1;
  103.     lose=(&n-1)-win-tie;
  104.   end;
  105.   else if cat=3 then do; /*cant determine who died 1st, use rehosp*/
  106.     win=cat1+cat2;
  107.     tie=cat3-1;
  108.     lose=(&n-1)-win-tie;
  109.   end;
  110.   check=win+lose+tie; /*check sum to n-1*/
  111.   u=win-lose; /*ties have zero weight*/
  112.   u2=u**2;
  113.   keep samp subjno trt win lose tie u u2 check ;
  114.   label win='No. of wins' lose='No of losses' tie='No. of ties'
  115.         u='Win-ratio U (sum of wins/losses/ties)' u2='U^2 (for var of test stat)'
  116.         check='Sum of wins/losses/ties (use as a check)';
  117. run;
  118.  
  119. proc sort data=&outdata;
  120.   by samp subjno;
  121. run;
  122.  
  123. ********************************************************************;
  124. ***                   p-value                         ***;
  125. ********************************************************************;
  126.  
  127. *calculate the p-value by hand for the unmatched win ratio
  128. (see Finkelstein and Schoenfeld, and pocock stat appendix: the U^2
  129.  and U are summed for the active grp. NOTE: there is a typo in
  130.  their variance eqn i.e. it should sum U^2 from 1 to N, not 1 to n);
  131.  
  132. proc univariate data=&outdata (where=(trt='Active'));
  133.   var u ;
  134.   by samp;
  135.   output out=usum sum=usum;
  136. run;
  137.  
  138. proc univariate data=&outdata;
  139.   var u2;
  140.   by samp;
  141.   output out=u2sum sum=u2sum;
  142. run;
  143.  
  144. data &outpval;
  145.   merge usum u2sum;
  146.   by samp;
  147.   T=usum; /*sum of U for active trt group*/
  148.   V=(&n/2)*(&n/2)/(&n*(&n-1))*u2sum; /*variance of T*/
  149.   Z=T/sqrt(V);
  150.   nValue1=2*(1-probnorm(abs(Z))); /*p-value*/
  151. run;
  152.  
  153. %mend derive_wr;
  154.  
  155.  
  156. ***end****************************************************************;
  157.  
RAW Paste Data