Guest User

Untitled

a guest
Nov 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. /*
  2. This macro creates a table of charateristics for the variables listed.
  3. It handles categorical, binary and continuous variables and produces and output dataset that can be further customized. No statistical information is
  4. included in this analysis
  5. */
  6.  
  7. /*Parameters to be set:
  8. dsetin - name of dataset to be analyzed
  9. cont = macro variable list of variable names, ie cont=age weight height
  10. cat=list of categorical variables ie cat=sex grade
  11. bin=binary variables, such as smoking now, smoking ever
  12. dsetout=name of output dataset
  13.  
  14. Run example at the end for a sample output dataset call sample_table_char in the work directory
  15. */
  16.  
  17.  
  18. *options mprint symbolgen;
  19. %macro table_char(dsetin, cont, cat, bin, dsetout);
  20.  
  21.  
  22. *delete old dataset;
  23.  
  24. proc datasets nodetails nolist;
  25. delete &dsetout;
  26. quit;
  27.  
  28. /****************************************************************
  29. Handle Categorical Variables
  30. ****************************************************************/
  31.  
  32. *loop through variable list;
  33. %let i=1;
  34. %do %while (%scan(&cat, &i, " ") ^=%str());
  35. %let var=%scan(&cat, &i, " ");
  36.  
  37. *Get format for variable;
  38. data _null_;
  39. set &dsetin;
  40. call symput("var_fmt", vformat(&var));
  41. run;
  42.  
  43.  
  44.  
  45. proc freq data=&dsetin noprint;
  46. table &var/missing out=tab_var;
  47. run;
  48.  
  49.  
  50.  
  51. data temp1;
  52. length categorical $200.; format categorical $200.;
  53. length value $200.; format value $200.;
  54.  
  55. set tab_var;
  56. percent=percent/100;
  57. categorical=put(&var., &var_fmt.);
  58. if _n_=1 then do;
  59. value=put(count, 8.)||"("||compress(put(percent, percent8.1))||")";
  60. order=2;
  61. output;
  62. order=1;
  63. value='';
  64. categorical=propcase(vlabel(&var.));
  65. output;
  66. end;
  67. else do;
  68. order=2;
  69. value=put(count, 8.)||"("||compress(put(percent, percent8.1))||")";
  70. output;
  71.  
  72. end;
  73.  
  74. keep categorical value order;
  75. run;
  76.  
  77. proc sort data=temp1 out=temp2 (drop=order); by order categorical; run;
  78.  
  79.  
  80. proc append base=&dsetout data=temp2;
  81. run;
  82.  
  83. *clean up;
  84.  
  85. proc datasets nodetails nolist;
  86. delete tab_var temp1 temp2;
  87. run; quit;
  88.  
  89. *Increment counter;
  90. %let i=%eval(&i+1);
  91. %end; *Categorical;
  92.  
  93. /****************************************************************
  94. Handle Continuous Variables
  95. ****************************************************************/
  96.  
  97. %let i=1;
  98. %do %while (%scan(&cont, &i, " ") ^=%str());
  99. %let var=%scan(&cont, &i, " ");
  100.  
  101.  
  102.  
  103. proc means data=&dsetin (rename=&var=vn) noprint;
  104. var vn;
  105. output out=table_var n= nmiss= mean= min= max= std= median= p25= p75= p90=/autoname;
  106. run;
  107.  
  108. *get label of variable for clean reporting;
  109. data _null_;
  110. set &dsetin;
  111. call symput("var_label", vlabel(&var));
  112. run;
  113.  
  114.  
  115.  
  116.  
  117. data temp1;
  118. length categorical $200.; format categorical $200.;
  119. format value $200.; length value $200.;
  120.  
  121. set table_var;
  122.  
  123. categorical="&var_label.";
  124. value=.;
  125. output;
  126.  
  127. categorical='Count(Missing)';
  128. value=put(vn_n, 5.)||"("||compress(put(vn_nmiss, 5.))||")";
  129. output;
  130.  
  131. categorical='Mean (SD)';
  132. value=put(vn_mean, 8.1)||"("||compress(put(vn_stddev, 8.1))||")";
  133. output;
  134.  
  135. categorical='Median (IQR)';
  136. value=put(vn_median, 8.1)||"("||compress(put(vn_p25, 8.1))||" - "||compress(put(vn_p75, 8.1))||")";
  137. output;
  138.  
  139. categorical='Range';
  140. value=put(vn_min, 8.1)||" - "||compress(put(vn_max, 8.1));
  141. output;
  142.  
  143. categorical='90th Percentile';
  144. value=put(vn_p90, 8.1);
  145. output;
  146.  
  147.  
  148.  
  149. keep categorical value;
  150. run;
  151.  
  152.  
  153. proc append base=&dsetout data=temp1;
  154. run;
  155.  
  156. *clean up;
  157.  
  158. proc datasets nodetails nolist;
  159. delete table_var temp1;
  160. run; quit;
  161.  
  162. *Increment counter;
  163. %let i=%eval(&i+1);
  164. %end; *Continuous;
  165.  
  166. /*****************************************************************
  167. Handle Binary Variables (only report 1s)
  168. *****************************************************************/
  169.  
  170. %let i=1;
  171. %do %while (%scan(&bin, &i, " ") ^=%str());
  172. %let var=%scan(&bin, &i, " ");
  173.  
  174.  
  175.  
  176. proc freq data=&dsetin noprint;
  177. table &var/missing out=tab_var;
  178. run;
  179.  
  180. data tab_var;
  181. set tab_var;
  182. where &var=1;
  183. run;
  184.  
  185. data temp1;
  186. length categorical $200.; format categorical $200.;
  187. length value $200.; format value $200.;
  188.  
  189. set tab_var;
  190. percent=percent/100;
  191. if _n_=1 then do;
  192. value=put(count, 8.)||"("||compress(put(percent, percent8.1))||")";
  193. order=1;
  194. categorical=propcase(vlabel(&var.));
  195. output;
  196. end;
  197. keep categorical value;
  198. run;
  199.  
  200.  
  201.  
  202.  
  203. proc append base=&dsetout data=temp1;
  204. run;
  205.  
  206. *clean up;
  207.  
  208. proc datasets nodetails nolist;
  209. delete tab_var temp1;
  210. run; quit;
  211.  
  212. *Increment counter;
  213. %let i=%eval(&i+1);
  214. %end;*Binary;
  215.  
  216.  
  217. %mend table_char;
  218.  
  219.  
  220. /* *Example of macro usage; */
  221. /* data sample; */
  222. /* set sashelp.class; */
  223. /* female=ifn( sex='F',1,0); */
  224. /* run; */
  225. /* */
  226. /* */
  227. /* %table_char(sample, height weight age, sex, female, sample_table_char); */
Add Comment
Please, Sign In to add comment