{"id":635,"date":"2021-03-01T14:51:28","date_gmt":"2021-03-01T19:51:28","guid":{"rendered":"http:\/\/blog.uvm.edu\/tbplante\/?p=635"},"modified":"2024-02-13T10:54:02","modified_gmt":"2024-02-13T15:54:02","slug":"table-1-with-pweights-in-stata","status":"publish","type":"post","link":"https:\/\/blog.uvm.edu\/tbplante\/2021\/03\/01\/table-1-with-pweights-in-stata\/","title":{"rendered":"Table 1 with pweights in Stata"},"content":{"rendered":"\n<p>The very excellent table1_mc program will automate generation of your Table 1 for nearly all needs (read about it <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.uvm.edu\/tbplante\/2019\/07\/11\/make-a-table-1-in-stata-in-no-time-with-table1_mc\/\" target=\"_blank\">here<\/a>), except for datasets using pweight. I&#8217;ve been toying around with automating Excel table generation using Stata v16+ Frames features. I recently started working on a database that requires pweighting for analyses, and opted to use this to as an opportunity to use Frames to generate the automation of a pweight adjusted Table 1.<\/p>\n\n\n\n<p>v1.3 of my code (updated 2024-2) to automate this lives here: <a href=\"https:\/\/www.uvm.edu\/~tbplante\/p_weight_table1_v1_3.do\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.uvm.edu\/~tbplante\/p_weight_table1_v1_3.do<\/a> <\/p>\n\n\n\n<p>You can just put:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>do https:\/\/www.uvm.edu\/~tbplante\/p_weight_table1_v1_3.do<\/code><\/pre>\n\n\n\n<p>&#8230;in your Stata do file and it&#8217;ll pull in the entire script!  <strong>Note that full instructions will show up in the Stata output window when you run the above line<\/strong>. The instructions below are incomplete. This code does not produce P-values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to use this do file<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Step 1a: close all open frames, drop all macros, \n\/\/          and open your dataset\nframes reset\nmacro drop _all\nwebuse multistage, clear\n\/\/\n\/\/ Step 1b: Figure out where your present working directory is, \n\/\/          this is where the excel spreadsheet will be saved. \n\/\/          Change the working directory with the \"cd\"\n\/\/          command as needed. \npwd\n\/\/\n\/\/ Step 2: Declare your data to be pweighted\nsvyset county &#091;pweight=sampwgt], strata(state) fpc(ncounties) || school, fpc(nschools)\n\/\/\n\/\/ Step 3: If your columns require the generation of pweighted \n\/\/         tertiles, quartiles, or whatnot, do that now. \n\/\/         For this example, we'll do by quartile of weight. \n\/\/ note: per this website: https:\/\/www.stata.com\/support\/faqs\/statistics\/percentiles-for-survey-data\/\n\/\/       ...Only the pweight needs to be specified when making \n\/\/        weighted quartiles. \nxtile weightquart=weight &#091;pweight=sampwgt], n(4) \n\/\/\n\/\/ Step 4: Recode binary variables so they are 0 and 1 (if needed)\n\/\/ Note: in this dataset, it's 1 and 2 for male and female, \n\/\/       respectively. \ngen female = 1 if sex==2 \/\/ recode sex to female, where 1 is female\nreplace female=0 if sex==1 \/\/ male is now 0\n\/\/\n\/\/ Step 5: Name your variables and options for multiple options\n\/\/ Note: The variables are already labeled but we are doing it \n\/\/       again for completeness' sake. \n\/\/\n\/\/ Continuous variables\nlabel variable weight \"Weight in lbs\"\nlabel variable height \"Height in in\" \/\/ I don't know why people are 400 in tall. that's 33 ft.\n\/\/ Nominal variables (same process would happen for ordinal or continuous varibles)\nlabel variable race \"Race\" \/\/ Race is nominal so need to also define values of race\nlabel define racelabels 1 \"White\" 2 \"Black\" 3 \"Other\"\nlabel values race racelabels \/\/ Apply the labels!!!\n\/\/ Binary variables, no need to apply labels\nlabel variable female \"Female sex\"\n\/\/\n\/\/ Step 6: Call the do file\n\/\/ Note: Instructions on this program's use will show right \n\/\/       after it's called. Look at the Stata output window. \ndo https:\/\/www.uvm.edu\/~tbplante\/p_weight_table1_v1_3.do\n\/\/\n\/\/ Step 7: Now follow the instructions! That are in the stata \n\/\/         output window!\ntable1pweight_start table1 1 4 weightquart weight %10.1f\ntable1pweight_contn_sd  table1 1 4 weightquart height %10.1f\ntable1pweight_bin  table1 1 4 weightquart female %10.0f\ntable1pweight_cat  table1 1 4 weightquart race %10.0f\ntable1pweight_end table1 1 4 weightquart weight %10.2f\n\/\/\n\/\/ CLOSE THE NEW EXCEL FILE OR YOU'LL GET AN ERROR WHEN \n\/\/ RUNNING STEP 7.\n\/\/\n\/\/ Step 8: Look at the excel output! Here, it's a file called \n\/\/         table1.xlsx that's sitting in  your pwd (see step \n\/\/         1b above). You might notice blanks for the 2nd and \n\/\/         3rd columns, but that's because of a strata with a single\n\/\/         sampling unit. You can confirm numbers using the survey\n\/\/         tools.\n\/\/ remember! This is where your excel file is saved:\npwd\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The very excellent table1_mc program will automate generation of your Table 1 for nearly all needs (read about it here), except for datasets using pweight. I&#8217;ve been toying around with automating Excel table generation using Stata v16+ Frames features. I recently started working on a database that requires pweighting for analyses, and opted to use &hellip; <a href=\"https:\/\/blog.uvm.edu\/tbplante\/2021\/03\/01\/table-1-with-pweights-in-stata\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Table 1 with pweights in Stata<\/span><\/a><\/p>\n","protected":false},"author":4473,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[477491],"tags":[677855,222041,678413,502556,678704,678040],"class_list":["post-635","post","type-post","status-publish","format-standard","hentry","category-stata-code","tag-biostatistics","tag-epidemiology","tag-pweight","tag-stata","tag-summary-statistics","tag-table-1"],"_links":{"self":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/635","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=635"}],"version-history":[{"count":15,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/635\/revisions"}],"predecessor-version":[{"id":1716,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/posts\/635\/revisions\/1716"}],"wp:attachment":[{"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/media?parent=635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/categories?post=635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.uvm.edu\/tbplante\/wp-json\/wp\/v2\/tags?post=635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}