TBL_Summary_Studymed.sas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /****************************************************************************************
  2. * Program Name : TBL_Summary_Studymed.sas *
  3. * Project Name : MKC-TI-005. *
  4. * Location : \\PDCSAS\SAS-DATA\STUDIES\studyname\SAS\PROGRAMS\TFLs *
  5. * Created By : Thomas Grzybowski *
  6. * Created On : 1-APR-2005 *
  7. * *
  8. * Description : Create Summary Statistics Table of exposure to study medication. *
  9. * *
  10. * Modification : *
  11. * By Date Description *
  12. * ---- ----------- -------------------------------------------------------------------- *
  13. * *
  14. * T. Grzybowski 1-June-2005 Use fdosdtn and ldosdtn as found in pop(derived) dataset.*
  15. * *
  16. * T. Grzybowski 1-Sept-2005 Modified to conform with new SAP, ie. add range, median *
  17. * and modify table column headers. *
  18. * *
  19. * T.Grzybowski 29SEP2005 Modified to satisfy new new table shell issued by Hao Ren, *
  20. * adding "Treatment Total" and Overall Total. *
  21. * *
  22. ****************************************************************************************/;
  23. options nofmterr symbolgen merror mlogic;
  24. *%setstudy(study=MKC-TI-005);
  25. %macro Contstats(dat=, var=, trtvar=, LABL=, SUFF=, BYGRP1= );
  26. PROC SORT DATA=&DAT;
  27. BY &TRTVAR &BYGRP1;
  28. RUN;
  29. /* stats against variable VAR using proc means */;
  30. PROC MEANS DATA=&DAT NOPRINT;
  31. BY &TRTVAR &BYGRP1;
  32. VAR &VAR;
  33. OUTPUT OUT=OUTMEAN&SUFF
  34. MEAN=Mean&SUFF
  35. MEDIAN =Med&SUFF
  36. MIN =Min&SUFF
  37. MAX =MAX&SUFF
  38. N =NUM&SUFF
  39. STD =STD&SUFF /autolabel; run;
  40. data outmean&SUFF;
  41. length range $11.;
  42. set outmean&SUFF;
  43. range = put(min&suffx,4.)||','||put(max&suffx,4.);
  44. drop min&suffx max&suffx;
  45. run;
  46. %mend Contstats;
  47. %macro postprocess(indat=, outdata=, Suffx= );
  48. /* transpose each row (of each i treatment + 1 for total) to create a column */;
  49. %do i = 1 %to &osumtrt;
  50. proc sql noprint;
  51. create table row&i as
  52. select
  53. mean&Suffx format 5.2,
  54. num&Suffx,
  55. med&Suffx,
  56. std&Suffx format 5.2,
  57. range
  58. from OUTMEAN&Suffx
  59. where trtc = "c&i";
  60. proc transpose data=row&i out=c&i;
  61. var mean&Suffx
  62. num&Suffx
  63. med&Suffx
  64. std&Suffx
  65. range;
  66. run;
  67. %end;
  68. /** coelesce columns into all as a single table */;
  69. proc sql;
  70. create table all_&suffx as
  71. select * from c1;
  72. %do i = 2 %to &Osumtrt;
  73. alter table all_&suffx
  74. add col&i char(20), subpop num;
  75. update all_&suffx set col&i=(select col1 from C&i where all_&suffx.._name_ = C&i.._name_);
  76. update all_&suffx set subpop=&suffx;
  77. %end;
  78. %mend postprocess;
  79. /*** Main macro calculates summary stats and produces a tabular report ***/;
  80. %macro tbl_summary_studymed();
  81. /* read-in population */;
  82. data pop;
  83. set derived.pop;
  84. length trtc $2.;
  85. trtc = "c"||put(trt,$1.);
  86. run;
  87. /* count treatment groups */;
  88. proc sql noprint;
  89. select count (distinct trt) into: numtrt from pop;
  90. %let tsumtrt = %eval(&numtrt+1);
  91. %let osumtrt = %eval(&numtrt+2);
  92. /* count pts per treatment group in the defined population */;
  93. %do i = 1 %to &numtrt;
  94. select count (distinct pt) into: ptcnt&i from pop where trtc = "c&i";
  95. %end;
  96. /* total pts in the population */;
  97. select count (distinct pt) into: ptcnt&Osumtrt from pop;
  98. /* "treatment total" pts in the population */;
  99. select count (distinct pt) into: ptcnt&Tsumtrt from pop where trtc > "c1";
  100. quit;
  101. /*** loop through specified populations ***/;
  102. %macro innerloop(pop=, suffx= );
  103. data pop&suffx;
  104. set pop;
  105. length trtc $2.;
  106. trtc = "c"||put(trt,$1.);
  107. run;
  108. %subset&pop;
  109. proc sql noprint;
  110. create table last_first_&suffx as
  111. select
  112. study,
  113. invsite,
  114. pt,
  115. trtc,
  116. fdosdtn,
  117. ldosdtn
  118. from pop&suffx
  119. where fdosdtn > 0;
  120. /* get days between first dose date and last dose date, make copy to use for total */;
  121. data last_first_&suffx;
  122. set last_first_&suffx;
  123. dffsd = (ldosdtn - fdosdtn) + 1;
  124. *if dffsd < 0 then dffsd = 0;
  125. run;
  126. /* create overall totals data */;
  127. data dup&suffx;
  128. set last_first_&suffx;
  129. trtc="c&Osumtrt";
  130. run;
  131. /* create treatment-totals data */;
  132. data trt&suffx;
  133. set last_first_&suffx;
  134. if trtc > "c1";
  135. trtc="c&Tsumtrt";
  136. run;
  137. proc append base=last_first_&suffx data=trt&suffx; run;
  138. proc append base=last_first_&suffx data=dup&suffx; run;
  139. proc sort data = last_first_&suffx;
  140. by study trtc dffsd; run;
  141. /*** call sub to generate descriptive statistics */;
  142. %contstats(dat=last_first_&suffx, var=dffsd, trtvar=trtc, LABL=A1, SUFF=&suffx, bygrp1=study);
  143. %postprocess(indat=OUTMEAN&suffx, outdata=all&suffx, suffx=&suffx);
  144. %mend innerloop;
  145. /* drive loop through populations */;
  146. %innerloop(pop=(varx=SFTY_POP,relx=eq,valx=1),suffx=1);
  147. %innerloop(pop=(varx=ITT_POP,relx=eq,valx=1),suffx=2);
  148. %innerloop(pop=(varx=PP_POP,relx=eq,valx=1), suffx=3);
  149. /* append results for report */;
  150. proc append base=all_1 data=all_2;
  151. proc append base=all_1 data=all_3;
  152. /* format text for report */;
  153. data allp;
  154. set all_1;
  155. length statno $3.;
  156. length var1 $10.;
  157. length statdesc $15;
  158. var1 = upcase(substr(_label_,1,9));
  159. shortname = upcase(substr(_name_,1,3));
  160. statno = put(shortname,$sn.);
  161. statdesc = put(shortname,$sndesc.);
  162. drop _label_ _name_ ;
  163. run;
  164. /* format to char and force left */;
  165. data allpp;
  166. %do i = 1 %to &osumtrt;
  167. length colc&i $16.;
  168. %end;
  169. set allp;
  170. %do i = 1 %to &osumtrt;
  171. colc&i = %str(col&i);
  172. colc&i = left(compress(colc&i));
  173. %end;
  174. run;
  175. /** Center decimal in col space */;
  176. data toreport;
  177. length visitdesc $20;
  178. %do i = 1 %to &osumtrt;
  179. length colcf&i $15.;
  180. %end;
  181. set allpp;
  182. %do i = 1 %to &osumtrt;
  183. point=indexc(colc&i,'.');
  184. if length(colc&i) = 1 then colcf&i = %str(" "||colc&i);
  185. if length(colc&i) = 2 then colcf&i = %str(" "||colc&i);
  186. if length(colc&i) = 3 then colcf&i = left(colc&i);
  187. if length(colc&i) > 3 then colcf&i = left(colc&i);
  188. if point = 4 then colcf&i = colc&i;
  189. if point = 3 then colcf&i = %str(" "||colc&i);
  190. if point = 2 then colcf&i = %str(" "||colc&i);
  191. if point = 1 then colcf&i = %str(" "||colc&i);
  192. if shortname = "RAN" then colcf&i = %str('['||trim(colc&i)||']');
  193. %end;
  194. if subpop = 1 then visitdesc = "Safety";
  195. if subpop = 2 then visitdesc = "Intention-to-Treat";
  196. if subpop = 3 then visitdesc = "Per Protocol";
  197. visit = 1;
  198. run;
  199. /* for col headings */;
  200. data _null_;
  201. %do i = 1 %to &osumtrt;
  202. call symput("TRTLAB&i", put(&i,$trtf.));
  203. %end;
  204. run;
  205. proc sort data=toreport;
  206. by subpop visitdesc statno; run;
  207. /*** The Report ***/;
  208. ODS listing; run;
  209. proc report data=toreport nowd split='*' missing headline formchar (2)='_' spacing=2 headskip;
  210. column subpop visitdesc statno statdesc (%do i=1 %to &osumtrt; (colcf&i) %end;);
  211. break before subpop /skip;
  212. define subpop / order noprint;
  213. define visitdesc / order width=20 'Population' left;
  214. define statno / order noprint;
  215. define statdesc / display width=14 'Statistic' left flow;
  216. %do i=1 %to &numtrt;
  217. define colcf&i /display width=11 left " &&trtlab&i* [N=%str(%cmpres(&&ptcnt&i))] ";
  218. %end;
  219. define colcf&tsumtrt /display width=11 left "Treatment*Total[2]* [N=%str(%cmpres(&&ptcnt&tsumtrt))] ";
  220. define colcf&osumtrt /display width=11 left " Overall* Total[2]* [N=%str(%cmpres(&&ptcnt&osumtrt))] ";
  221. run;
  222. ODS listing close;
  223. %mend tbl_summary_studymed;