Announcement

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

  • Struggling with table

    I would like to be able to produce a simple "Table 1" with baseline values of a couple of patient groups. That means listing a number of variables as rows with frequencies and percentages, divided into two columns by a group variable. But I am struggling.

    Why doesn't the following code work?
    • If run 'as is', it will not work. I receive an error message
    • If I leave out one of the 'statistic' options (no matter which) within the loop, the aggregated table will show, provided that no 'statistics' options are present in the first tabulation (cprSex). Sometimes the cprSex variable will be included in the table, but not always
    What is wrong? I have tried to browse through the many pages of 'table help', but can't figure it out.
    Thanks for any help.

    Code:
    local predAlle_tab "cprSex BMIkat genstande arvelighed aldersGrp rygning asa performance CCIkat lokalisationRectum mdt_konf afdnummerVol region"
        
    
    quietly table cprSex OverTx, statistic(freq) statistic(perc, across(OverTx))
    
    foreach var in `predAlle_tab' {
        quietly table `var' OverTx, statistic(freq) statistic(perc, across(OverTx)) append
        }
        
    collect layout (`predAlle_tab') (OverTx)

  • #2
    Check out dtable in Stata 18. It should have all of what you want to produce.
    ​​​
    In regards to your code, you are not collecting your table results into the collect command. Collect works by storing/collecting estimation results first, and then assembling those results according to user specifications. You should check the help files for collect and also collect get if you're curious about learning to build your own table of results with the command.

    For the purpose of making a table 1 descriptive table though, dtable is probably sufficient. Alternatively, try installing table1_mc from SSC in older versions of Stata.

    Comment


    • #3
      It helps when we have a dataset to go with the code.

      There are 2 problems with your code.

      1. You call table for the row variable cprSex twice.

      2. Your layout does not contain the result dimension. After the
      first call to table, use collect layout to see that the
      result dimension in interacted with the row variable.

      In the following I try to reproduce the spirit of your code using some
      publicly available data. I changed the loop to start off with an empty
      append macro, and fill it with "append" after the first call to
      table. Also, following the logic of the original calls to
      table, I interact the result dimension with the row
      variables in the final call to collect layout.

      Code:
      webuse nhanes2l
      
      unab colvar : region
      unab rowvars : hsizgp rural agegrp highlead highbp
      
      local append
      foreach var of local rowvars {
          quietly ///
          table `var' `colvar', ///
              statistic(freq) ///
              statistic(percent, across(`colvar')) ///
              `append'
          collect layout
          local append append
      }
      
      collect layout ((`rowvars')#result) (`colvar')
      Here is the final table.
      Code:
      ---------------------------------------------------------------------
                                  |                  Region
                                  |     NE      MW       S       W    Total
      ----------------------------+----------------------------------------
      # in household or 5 if #>=5 |
        1                         |
          Frequency               |    316     527     445     441    1,729
          Percent                 |  18.28   30.48   25.74   25.51   100.00
        2                         |
          Frequency               |    688     949     971   1,022    3,630
          Percent                 |  18.95   26.14   26.75   28.15   100.00
        3                         |
          Frequency               |    410     429     519     384    1,742
          Percent                 |  23.54   24.63   29.79   22.04   100.00
        4                         |
          Frequency               |    338     433     449     356    1,576
          Percent                 |  21.45   27.47   28.49   22.59   100.00
        5                         |
          Frequency               |    344     436     469     425    1,674
          Percent                 |  20.55   26.05   28.02   25.39   100.00
        Total                     |
          Frequency               |  2,096   2,774   2,853   2,628   10,351
          Percent                 |  20.25   26.80   27.56   25.39   100.00
      Rural                       |
        Urban                     |
          Frequency               |  1,513   1,858   1,491   1,686    6,548
          Percent                 |  23.11   28.38   22.77   25.75   100.00
        Rural                     |
          Frequency               |    583     916   1,362     942    3,803
          Percent                 |  15.33   24.09   35.81   24.77   100.00
        Total                     |
          Frequency               |  2,096   2,774   2,853   2,628   10,351
          Percent                 |  20.25   26.80   27.56   25.39   100.00
      Age group                   |
        20–29                     |
          Frequency               |    444     684     589     603    2,320
          Percent                 |  19.14   29.48   25.39   25.99   100.00
        30–39                     |
          Frequency               |    334     433     443     412    1,622
          Percent                 |  20.59   26.70   27.31   25.40   100.00
        40–49                     |
          Frequency               |    246     376     365     285    1,272
          Percent                 |  19.34   29.56   28.69   22.41   100.00
        50–59                     |
          Frequency               |    292     307     385     307    1,291
          Percent                 |  22.62   23.78   29.82   23.78   100.00
        60–69                     |
          Frequency               |    587     721     811     741    2,860
          Percent                 |  20.52   25.21   28.36   25.91   100.00
        70+                       |
          Frequency               |    193     253     260     280      986
          Percent                 |  19.57   25.66   26.37   28.40   100.00
        Total                     |
          Frequency               |  2,096   2,774   2,853   2,628   10,351
          Percent                 |  20.25   26.80   27.56   25.39   100.00
      High lead level             |
        lead<25                   |
          Frequency               |    932   1,226   1,302   1,195    4,655
          Percent                 |  20.02   26.34   27.97   25.67   100.00
        lead>=25                  |
          Frequency               |     67      93      62      71      293
          Percent                 |  22.87   31.74   21.16   24.23   100.00
        Total                     |
          Frequency               |    999   1,319   1,364   1,266    4,948
          Percent                 |  20.19   26.66   27.57   25.59   100.00
      High blood pressure         |
        0                         |
          Frequency               |  1,172   1,664   1,636   1,503    5,975
          Percent                 |  19.62   27.85   27.38   25.15   100.00
        1                         |
          Frequency               |    924   1,110   1,217   1,125    4,376
          Percent                 |  21.12   25.37   27.81   25.71   100.00
        Total                     |
          Frequency               |  2,096   2,774   2,853   2,628   10,351
          Percent                 |  20.25   26.80   27.56   25.39   100.00
      ---------------------------------------------------------------------

      Comment

      Working...
      X