Announcement

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

  • Problem appending multiple "table (varlist) (command result)" code using collect layout

    I am trying to append these tables but to no avail. Your solution will be much appreciated. My stata version is 17.

    Code:
    table (state period progexp) (command result) if savemoney==1 & earnmoney==1 & newsaver!=1, command(r(mean) r(lb) r(ub):ci means currsavings, level(95)) nformat(%5.2f `r(mean)' `r(lb)' `r(ub)') nototals
    table (state period progexp) (command result) if  savemoney==1 & earnmoney==1 & newsaver==1, command(r(mean) r(lb) r(ub):ci means currsavings, level(95)) nformat(%5.2f `r(mean)' `r(lb)' `r(ub)') nototals append
    collect layout (state#period#progexp#command) (result)
    Last edited by Kehinde Atoloye; 15 Jun 2023, 04:31.

  • #2
    We do not have access to your dataset, so I will switch to the auto data and simplify groups by using rep78 instead of state period progexp.

    Also the specification in option nformat() is using macro expansion on the requested results (usually yielding values after the command, but nothing in this example) instead of result names. I "fix" that in the following example too.

    When appending collections from table you will need to update your layout to include a dimension that identifies each call to table. That is the purpose of dimension cmdset.

    Code:
    sysuse auto
    
    table (rep78) (command result) if foreign==0, ///
            command(r(mean) r(lb) r(ub):ci means mpg, level(95)) ///
            nformat(%5.2f mean lb ub) ///
            nototals
    * show that cmdset has one level -- 1
    collect levelsof cmdset
    
    table (rep78) (command result) if foreign==1, ///
            append ///
            command(r(mean) r(lb) r(ub):ci means mpg, level(95)) ///
            nformat(%5.2f mean lb ub) ///
            nototals
    * show that cmdset now has two levels -- 1 and 2
    collect levelsof cmdset
    
    * give meaningful labels to the levels of cmdset
    collect label values cmdset 1 "Domestic" 2 "Foreign"
    
    collect layout (rep78#command#cmdset) (result)
    Here is the resulting table.
    Code:
    . collect layout (rep78#command#cmdset) (result)
    
    Collection: Table
          Rows: rep78#command#cmdset
       Columns: result
       Table 1: 19 x 3
    
    ----------------------------------------------------------------------------
                                |   Mean   Lower bound of CI   Upper bound of CI
    ----------------------------+-----------------------------------------------
    Repair record 1978          |                                               
      1                         |                                               
        ci means mpg, level(95) |                                               
          Domestic              |  21.00              -17.12               59.12
      2                         |                                               
        ci means mpg, level(95) |                                               
          Domestic              |  19.12               15.98               22.27
      3                         |                                               
        ci means mpg, level(95) |                                               
          Domestic              |  19.00               17.38               20.62
          Foreign               |  23.33               17.08               29.58
      4                         |                                               
        ci means mpg, level(95) |                                               
          Domestic              |  18.44               14.92               21.97
          Foreign               |  24.89               22.80               26.97
      5                         |                                               
        ci means mpg, level(95) |                                               
          Domestic              |  32.00                6.59               57.41
          Foreign               |  26.33               19.13               33.53
    ----------------------------------------------------------------------------

    Comment


    • #3
      Originally posted by Jeff Pitblado (StataCorp) View Post
      We do not have access to your dataset
      Thank you. Here is my dataset. For some reasons unknown to me, the -cmdset- does not increase the level count to 2 for the second table. Hence no appending occurred. Surprisingly, your example worked successfully with your data on my system. Is there something I am not doing right?
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte state float period byte(progexp savemoney earnmoney) float newsaver long currsavings
      2 1 1 1 1 .  23500
      2 1 1 1 1 .  20000
      2 0 1 . 1 .      .
      2 1 1 1 1 .  15000
      2 0 1 1 1 .      .
      2 1 1 1 1 .  20000
      2 0 1 0 1 .      .
      2 0 1 . 0 .      .
      2 1 1 1 1 .  20000
      2 0 1 1 1 .   3000
      2 1 1 1 1 .  80000
      2 0 1 1 1 .      .
      2 0 1 . 1 .      .
      2 0 1 1 1 .      .
      2 0 1 1 1 .  27000
      2 0 1 1 1 .   5000
      2 1 1 1 1 .  53000
      2 1 1 1 1 .   5000
      2 0 1 1 1 .  50000
      2 1 1 1 1 .  35000
      2 0 1 . 0 .      .
      2 0 1 . 0 .      .
      2 1 1 1 1 .   5000
      2 1 1 1 1 .  30000
      2 0 1 . 0 .      .
      2 0 1 . 0 .      .
      2 0 1 . 0 .      .
      2 1 1 1 1 .   2000
      2 0 1 . 0 .      .
      2 1 1 1 1 .  10000
      2 1 1 1 1 .  25000
      2 0 1 1 1 .  40000
      2 1 1 1 1 .  50000
      2 0 1 1 1 .  50000
      2 1 1 1 1 .  17500
      2 0 1 1 1 .   2000
      2 0 1 . 0 .      .
      2 0 1 . 0 .      .
      2 1 1 1 1 .  32000
      2 1 1 1 1 .  70000
      2 0 1 1 1 .      .
      2 1 1 1 1 .      .
      2 0 1 . 0 .      .
      2 0 1 . 1 .      .
      2 1 1 1 1 .  26000
      2 0 1 . 0 .      .
      2 1 1 1 1 .  10000
      2 0 1 . 0 .      .
      2 1 1 1 1 .  70000
      2 0 1 . 0 .      .
      2 1 1 1 1 .  14500
      2 0 1 . 0 .      .
      2 0 1 . 0 .      .
      2 0 1 . 0 .      .
      2 1 1 1 1 .  18000
      2 0 1 . 0 .      .
      2 0 1 . 0 .      .
      2 1 1 1 1 .  10000
      2 0 1 . 1 .      .
      2 0 1 . 0 .      .
      2 1 1 1 1 .  30000
      2 0 1 1 1 .  10000
      2 1 1 1 1 .  70000
      2 0 1 . 0 .      .
      2 1 1 1 1 .  33000
      2 1 1 1 1 .  16000
      2 0 1 . 0 .      .
      2 1 1 1 1 . 120000
      2 0 1 . 0 .      .
      1 1 1 1 1 .  11900
      1 0 1 . 1 .      .
      1 1 1 1 1 .  12000
      2 0 1 . 0 .      .
      2 0 1 1 1 .   5000
      1 0 1 . 0 .      .
      1 1 1 1 1 .   1500
      1 0 1 . 0 .      .
      1 0 1 . 0 .      .
      1 1 1 1 1 . 100000
      1 0 1 . 0 .      .
      1 1 1 1 1 .   2000
      1 0 1 . 0 .      .
      1 1 1 1 1 .  20000
      1 1 1 1 1 .  30000
      1 0 1 . 0 .      .
      1 1 1 1 1 .  40000
      2 0 1 . 1 .      .
      2 0 1 1 1 .   2000
      2 0 1 . 0 .      .
      2 1 1 1 1 .   7000
      2 0 1 1 1 .   5000
      2 1 1 1 1 .  70000
      2 0 1 . 0 .      .
      2 0 1 . 1 .      .
      2 1 1 1 1 .  17000
      2 0 1 1 1 .  25000
      2 1 1 1 1 .  10000
      2 0 1 1 1 .      .
      2 1 1 1 1 .  20000
      2 0 1 1 1 .  20000
      end
      Code:
      table (state period progexp) (command result) if savemoney==1 & earnmoney==1 & newsaver!=1, command(r(mean) r(lb) r(ub):ci means currsavings, level(95)) nformat(%5.2f mean lb ub) nototals
      * show that cmdset has one level -- 1
      collect levelsof cmdset
      table (state period progexp) (command result) if savemoney==1 & earnmoney==1 & newsaver==1, command(r(mean) r(lb) r(ub):ci means currsavings, level(95)) nformat(%5.2f mean lb ub) nototals
      * show that cmdset now has two levels -- 1 and 2
      collect levelsof cmdset
      * give meaningful labels to the levels of cmdset
      collect label values cmdset 1 "a" 2 "b"
      collect layout (state#period#progexp#command#cmdset) (result)
      Below is my output:
      Code:
      . table (state period progexp) (command result) if savemoney==1 & earnmoney==1 & newsaver!=1, command(r(mean) r(lb) r(ub):ci means currsavings, level(95)) nfor
      > mat(%5.2f mean lb ub) nototals
      
      --------------------------------------------------------------------------------
                                   |           ci means currsavings, level(95)        
                                   |      Mean   Lower bound of CI   Upper bound of CI
      -----------------------------+--------------------------------------------------
      State                        |                                                  
        Ogun                       |                                                  
          Period of Study          |                                                  
            Baseline               |                                                  
              Program Exposure     |                                                  
                Comparison Group   |   8922.58             6561.03            11284.13
                Intervention Group |  13222.22             5605.25            20839.19
            Endline                |                                                  
              Program Exposure     |                                                  
                Comparison Group   |  14381.91            11505.43            17258.40
                Intervention Group |  13076.46            11634.04            14518.87
        Kaduna                     |                                                  
          Period of Study          |                                                  
            Baseline               |                                                  
              Program Exposure     |                                                  
                Comparison Group   |  12907.02             7714.99            18099.04
                Intervention Group |  18103.66             9874.21            26333.11
            Endline                |                                                  
              Program Exposure     |                                                  
                Comparison Group   |  19115.89            13523.94            24707.85
                Intervention Group |  15278.41            13061.34            17495.48
      --------------------------------------------------------------------------------
      
      . * show that cmdset has one level -- 1
      . collect levelsof cmdset
      
      Collection: Table
       Dimension: cmdset
          Levels: 1
      
      . table (state period progexp) (command result) if savemoney==1 & earnmoney==1 & newsaver==1, command(r(mean) r(lb) r(ub):ci means currsavings, level(95)) nfor
      > mat(%5.2f mean lb ub) nototals
      
      --------------------------------------------------------------------------------
                                   |           ci means currsavings, level(95)        
                                   |      Mean   Lower bound of CI   Upper bound of CI
      -----------------------------+--------------------------------------------------
      State                        |                                                  
        Ogun                       |                                                  
          Period of Study          |                                                  
            Endline                |                                                  
              Program Exposure     |                                                  
                Comparison Group   |  17666.67            -1.3e+04            48726.67
                Intervention Group |  25000.00            -3.9e+04            88531.02
        Kaduna                     |                                                  
          Period of Study          |                                                  
            Endline                |                                                  
              Program Exposure     |                                                  
                Comparison Group   |  10886.67             4837.47            16935.86
                Intervention Group |   8295.65             4554.19            12037.12
      --------------------------------------------------------------------------------
      
      . * show that cmdset now has two levels -- 1 and 2
      . collect levelsof cmdset
      
      Collection: Table
       Dimension: cmdset
          Levels: 1
      
      . * give meaningful labels to the levels of cmdset
      . collect label values cmdset 1 "Domestic" 2 "Foreign"
      
      . collect layout (state#period#progexp#command#cmdset) (result)
      
      Collection: Table
            Rows: state#period#progexp#command#cmdset
         Columns: result
         Table 1: 25 x 3
      
      -----------------------------------------------------------------------------------------------
                                                  |      Mean   Lower bound of CI   Upper bound of CI
      --------------------------------------------+--------------------------------------------------
      State                                       |                                                  
        Ogun                                      |                                                  
          Period of Study                         |                                                  
            Endline                               |                                                  
              Program Exposure                    |                                                  
                Comparison Group                  |                                                  
                  ci means currsavings, level(95) |                                                  
                    Command results index         |                                                  
                      Domestic                    |  17666.67            -1.3e+04            48726.67
                Intervention Group                |                                                  
                  ci means currsavings, level(95) |                                                  
                    Command results index         |                                                  
                      Domestic                    |  25000.00            -3.9e+04            88531.02
        Kaduna                                    |                                                  
          Period of Study                         |                                                  
            Endline                               |                                                  
              Program Exposure                    |                                                  
                Comparison Group                  |                                                  
                  ci means currsavings, level(95) |                                                  
                    Command results index         |                                                  
                      Domestic                    |  10886.67             4837.47            16935.86
                Intervention Group                |                                                  
                  ci means currsavings, level(95) |                                                  
                    Command results index         |                                                  
                      Domestic                    |   8295.65             4554.19            12037.12
      -----------------------------------------------------------------------------------------------

      Comment


      • #4
        You forgot to add option append in your second call to table.

        Comment

        Working...
        X