{"id":12,"date":"2017-05-31T14:28:05","date_gmt":"2017-05-31T18:28:05","guid":{"rendered":"http:\/\/blog.uvm.edu\/tbplante\/?p=12"},"modified":"2019-07-12T12:21:42","modified_gmt":"2019-07-12T16:21:42","slug":"table-1-program","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2017\/05\/31\/table-1-program\/","title":{"rendered":"Table 1 program"},"content":{"rendered":"<h1>July 2019 update<\/h1>\n<p>I have since discovered the excellent table1_mc program and have abandoned using this program. I&#8217;m keeping this code here for historical sake. <a href=\"http:\/\/blog.uvm.edu\/tbplante\/2019\/07\/11\/make-a-table-1-in-stata-in-no-time-with-table1_mc\/\">Check out my write-up of table1_mc<\/a>.<\/p>\n<h1>Stata programs to make tables<\/h1>\n<p>Here are a few simple Stata programs that will write a CSV file for your Table 1.<\/p>\n<p>This has three parts:<\/p>\n<ol>\n<li>Header (writes the first line of the table with names and the row with Ns),<\/li>\n<li>Program for continuous variables, and<\/li>\n<li>Program for dichotomous variables.<\/li>\n<\/ol>\n<p>Each time you want to add another line your table, just call the appropriate program followed by the variable of interest.<\/p>\n<pre>clear all \/\/ get rid of everything in memory\nwebuse auto.dta, clear\n\nset seed 12345\ngen treatment = .\nreplace treatment = round(runiform()) \/\/ make a random treatment \n\/\/ variable that's 0 or 1\n\n\/\/ Header + second row with Ns\nquietly {\ncapture log close table1 \/\/ force closes any tables with the same name\nlog using \"my_table_1.csv\", text replace name(table1) \/\/replace will erase \n\/\/ any CSV files you started already with the same name\nnoisily disp \",All,Group 1,Group 2\" \/\/ line 1\ncount\nlocal nall=r(N)\ncount if treatment==0\nlocal ntreatment0=r(N)\ncount if treatment==1\nlocal ntreatment1=r(N)\nnoisily disp \"N,\" `nall' \",\" `ntreatment0' \",\" `ntreatment1'\nlog close table1\n}\n\n\/\/ Program for continuous variables\ncapture program drop table1_cont \/\/ drops any programs with the same name\nprogram define table1_cont\nquietly {\nsyntax varlist\ncapture log close table1\nlog using \"my_table_1.csv\", text append name(table1) \/\/ append will \n\/\/ keep writing onto existing tables\nforeach var of varlist `varlist' {\nsum `var'\nlocal `var'mean = r(mean)\nlocal `var'sd = r(sd)\nlocal `var'n=r(N)\nsum `var' if treatment==0\nlocal `var'mean0 = r(mean)\nlocal `var'sd0 = r(sd)\nlocal `var'n0=r(N)\nsum `var' if treatment==1\nlocal `var'mean1 = r(mean)\nlocal `var'sd1 = r(sd)\nlocal `var'n1=r(N)\n\nnoisily disp \"`var' (Mean (SD)),\" \/\/\/\n%3.1f ``var'mean' \" (\" %3.1f ``var'sd' \"),\"\u00a0 \/\/\/\n\u00a0%3.1f ``var'mean0' \" (\" %3.1f ``var'sd0' \"),\"\u00a0 \/\/\/\n\u00a0%3.1f ``var'mean1' \" (\" %3.1f ``var'sd1' \")\"\n} \/\/ end varlist loop\nlog close table1\n} \/\/ end quietly\nend\n\n\/\/ program for dichotomous variables\ncapture program drop table1_dichotomous\nprogram define table1_dichotomous\nquietly {\nsyntax varlist\ncapture log close table1\nlog using \"my_table_1.csv\", text append name(table1)\nforeach var of varlist `varlist' {\nsum `var'\nlocal `var'n= r(N)\nlocal `var'mean = r(mean)*100\nsum `var' if treatment==0\nlocal `var'n0 = r(N)\nlocal `var'mean0 = r(mean)*100\nsum `var' if treatment==1\nlocal `var'n1 = r(N)\nlocal `var'mean1 = r(mean)*100\nnoisily disp \"`var' (N (%)),\" \/\/\/\n``var'n' \" (\" %3.1f ``var'mean' \"),\"\u00a0 \/\/\/\n``var'n0' \" (\" %3.1f ``var'mean0' \"),\" \/\/\/\n``var'n1' \" (\" %3.1f ``var'mean1' \")\" \n} \nlog close table1 \n} \nend \n\/\/ now just call these programs as needed: \n\ntable1_cont trunk \ntable1_cont weight \ntable1_dichotomous foreign \n\/\/ and so-on\n<\/pre>\n<p>Here is the output from the example above:<\/p>\n<p style=\"padding-left: 30px\">,All,Group 1,Group 2<br \/>\nN,74,43,31<br \/>\ntrunk (Mean (SD)),13.8 (4.3),13.0 (3.9),14.8 (4.6)<br \/>\nweight (Mean (SD)),3019.5 (777.2),2906.0 (746.1),3176.8 (804.1)<br \/>\nforeign (N (%)),74 (29.7),43 (32.6),31 (25.8)<\/p>\n<p>If opened with MS Excel, it will look like this:<\/p>\n<p style=\"padding-left: 30px\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20 size-full\" src=\"http:\/\/blog.uvm.edu\/tbplante\/files\/2017\/05\/Capture.png\" alt=\"\" width=\"469\" height=\"145\" srcset=\"https:\/\/blog.uvm.edu\/tbplante\/files\/2017\/05\/Capture.png 469w, https:\/\/blog.uvm.edu\/tbplante\/files\/2017\/05\/Capture-300x93.png 300w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>July 2019 update I have since discovered the excellent table1_mc program and have abandoned using this program. I&#8217;m keeping this code here for historical sake. Check out my write-up of table1_mc. Stata programs to make tables Here are a few simple Stata programs that will write a CSV file for your Table 1. This has &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2017\/05\/31\/table-1-program\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Table 1 program<\/span><\/a><\/p>\n","protected":false},"author":4473,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"footnotes":""},"categories":[477491],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-image","hentry","category-stata-code","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/users\/4473"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":9,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":299,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/12\/revisions\/299"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}