Announcement

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

  • Tabout: produce a table of means with multiple variables

    Dear Statalisters,

    This is my first post, therefore I hope it will be comprehensible and compliant to the rules.

    I would like to use the command tabout in Stata16 on Windows10 to generate a table containing the mean of 5 variables.
    In order to be more clear, I'd like to make an example with a common dataset.

    > sysuse auto
    > tabulate foreign rep78, summarize (price) means
    > tabout foreign rep78 using giorgio.txt, replace c(mean price) sum format (2 2) clab(_ _) layout(rb) h3(nil) style(tex) bt font(bold) topstr(11cm)

    What I have here is quite clear and simple for me, I cross-tabulated foreign and rep78 with the table containing values of the mean of price.

    What if I would be interested in tabulating and then exporting a table which contains not only the mean of price but, for example, also the mean of the weight of the cars?

    As I understand I am not good with words, I am gonna show below a short example of what I would need.
    repair record 1978 1 2 3 4 5 total
    car type
    mean of price domestic
    foreign
    mean of weight domestic
    foreign
    Do you think this is possible?

    Thank you in advance for all your answers and I hope I was clear enough

    Giorgio

  • #2
    I do not use tabout (SSC), but here is a way to get what you want in a dataset. You can export in whichever format you want.

    Code:
    *WANTED
    sysuse auto, clear
    foreach var in price weight mpg{
        tabulate foreign rep78, summarize (`var') means
    }
    
    *CODE:
    clear
    tempfile a1
    gen var=""
    save `a1'
    sysuse auto, clear
    foreach var in mpg weight price{
        separate `var', by(rep78) gen(cat)
        drop if missing(rep78)
        egen total= rowtotal(cat?)
        collapse cat? total, by(foreign)
        gen var= "`var'"
        append using `a1'
        save `a1', replace
        sysuse auto, clear
    }
    use `a1', clear
    order var
    l, sepby(var)
    Res.:

    Code:
    . foreach var in price weight mpg{
      2.
    .     tabulate foreign rep78, summarize (`var') means
      3.
    . }
    
    
                                  Means of Price
    
               |                Repair Record 1978
      Car type |         1          2          3          4          5 |     Total
    -----------+-------------------------------------------------------+----------
      Domestic |   4,564.5  5,967.625  6,607.074  5,881.556    4,204.5 |  6,179.25
       Foreign |         .          .  4,828.667  6,261.444  6,292.667 | 6,070.143
    -----------+-------------------------------------------------------+----------
         Total |   4,564.5  5,967.625  6,429.233    6,071.5      5,913 | 6,146.043
    
                              Means of Weight (lbs.)
    
               |                Repair Record 1978
      Car type |         1          2          3          4          5 |     Total
    -----------+-------------------------------------------------------+----------
      Domestic |     3,100   3,353.75  3,442.222  3,532.222      1,960 | 3,368.333
       Foreign |         .          .      2,010  2,207.778  2,403.333 | 2,263.333
    -----------+-------------------------------------------------------+----------
         Total |     3,100   3,353.75      3,299      2,870  2,322.727 | 3,032.029
    
                              Means of Mileage (mpg)
    
               |                Repair Record 1978
      Car type |         1          2          3          4          5 |     Total
    -----------+-------------------------------------------------------+----------
      Domestic |        21     19.125         19  18.444444         32 | 19.541667
       Foreign |         .          .  23.333333  24.888889  26.333333 | 25.285714
    -----------+-------------------------------------------------------+----------
         Total |        21     19.125  19.433333  21.666667  27.363636 | 21.289855
    
    
    . l, sepby(var)
    
         +--------------------------------------------------------------------------------+
         |    var    foreign      cat1      cat2      cat3      cat4      cat5      total |
         |--------------------------------------------------------------------------------|
      1. |  price   Domestic   4,564.5   5,967.6   6,607.1   5,881.6   4,204.5    6179.25 |
      2. |  price    Foreign         .         .   4,828.7   6,261.4   6,292.7   6070.143 |
         |--------------------------------------------------------------------------------|
      3. | weight   Domestic     3,100   3,353.8   3,442.2   3,532.2     1,960   3368.333 |
      4. | weight    Foreign         .         .     2,010   2,207.8   2,403.3   2263.333 |
         |--------------------------------------------------------------------------------|
      5. |    mpg   Domestic        21    19.125        19   18.4444        32   19.54167 |
      6. |    mpg    Foreign         .         .   23.3333   24.8889   26.3333   25.28572 |
         +--------------------------------------------------------------------------------+
    Last edited by Andrew Musau; 28 Dec 2020, 15:59.

    Comment


    • #3
      Thank you very much for your answer Andrew, I would never have been able to produce a code like that.

      Comment


      • #4
        Dear Andrew, I tried in these days to export the table on PNG. If I have to be honest, I am not able to work with the temporary file.

        Comment


        • #5
          You can export as a MS Word or MS Excel file. Here is a way to do the former using putdocx. The code is modified to use variable labels and some variables are renamed.

          Code:
          tempfile a1
          gen Variable=""
          save `a1'
          sysuse auto, clear
          foreach var in mpg weight price{
              gen Variable= "`:var lab `var''"
              separate `var', by(rep78) gen(_)
              drop if missing(rep78)
              egen Total= rowtotal(_?)
              collapse _? Total, by(foreign Var)
              append using `a1'
              save `a1', replace
              sysuse auto, clear
          }
          use `a1', clear
          rename foreign Origin
          order Var
          putdocx begin
          putdocx table mytable = data(*), varnames width(100%)
          putdocx save myfile.docx, replace
          Res.:

          Click image for larger version

Name:	Untitled.png
Views:	1
Size:	25.5 KB
ID:	1588080

          Comment

          Working...
          X