Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Summarize continuous variables by binary variable in -tabout-

    It seems that this topic has been discussed before, but I thought I would ask if anyone has a decent workaround for the following problem. If I've missed an answer to this question elsewhere, my apologies. I am doing a standard descriptive analysis and am using the -tabout- feature to create tables in LaTeX that report the N and % of binary or categorical variables by a binary 'case' variable (case or control). I want to add to this table the means and SDs of continuous variables, also by this binary 'case' variable.

    The final result should ideally look like the following (In order to easily follow along, I am using the 'auto' dataset):
    Characteristic Foreign Domestic
    Repair record 1978: N (%)
    1 0 (0) 2 (4.2)
    2 0 (0) 8 (16.7)
    3 3 (14.3) 27 (56.3)
    4 9 (42.9) 9 (18.8)
    5 9 (42.9) 2 (4.2)
    Miles per gallon: mean +/- SD 24.8 +/- 6.6 19.8 +/- 4.7
    LaTeX code works pretty well with the categorical variable (in this case, [rep78]):
    tabout rep78 foreign using "file.txt", replace style(tex) font(bold) bt cells(freq col) layout(col) format(1) ptotal(none)

    However, attempting to summarize continuous variables by levels of a binary variable (as in the last row of the table, for [mpg]) proves difficult. I believe there may be a workaround by using 'do' loops to write tables within tables, but am not sure how to code this. I also know there are workarounds that provide levels of the continuous variable in the columns and levels of the binary variable in rows, but I do not want to orient my table that way.

    Hope the question is clear, and I will be eternally grateful for any advice you can give.

    Last edited by Maria Sundaram; 30 Jun 2016, 01:28. Reason: (edited to add tags. If it's not clear I am a statalist newbie, this should tip you off)

  • #2
    Actually, I know two commands that do exactly what you are looking for: -table1- and -basetable- (SSC).
    But one way to build your table with tabout is something like this:
    Code:
    sysuse auto, clear
    !erase file.xls
    generate average = 1
    generate sd= 1
    foreach i of var price mpg weight rep78 {
        if "`i'"=="rep78" {
            tabout `i' foreign using file.xls, ///
        c(col) layout(cb) noff(3) clab(Col%) h1(nil) npos(row) show(none) app
        }
            else {
            label define average 1 "`: var label `i'': mean", modify
            label val average average
            label define sd 1 "`: var label `i'': sd", modify
            label val sd sd
            tabout average foreign using file.xls,  c(mean mpg) ///
            f(3c) sum lines(none) ptotal(none) show(none) app
            tabout sd foreign using file.xls,  c(sd mpg) ///
            f(3c) h1(nil) h2(nil) h3(nil)  ptotal(none) sum lines(none) show(none) app
        }
    }

    Comment


    • #3
      Hi Oded, thank you for your answer! I ran a slightly modified version of your code, changing the .xls file formats to .txt for LaTeX. I still had some formatting issues, but I am sure eventually these could be taken care of in LaTeX. However, I still have difficulty showing mean and sd in the same row for the same variable. Once I use the option c(mean mpg sd mpg), the table reverts to showing binary variables (domestic, e.g.) in the rows, and the continuous variables (mpg, weight, etc.) in the columns. Would you have any additional advice on how to re-orient that table so that it can be shown with continuous variables as rows, and the binary variables (domestic, e.g.) in the columns?

      screenshot.png

      Many thanks again,
      Maria
      Last edited by Maria Sundaram; 30 Jun 2016, 19:47.

      Comment

      Working...
      X