Announcement

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

  • dtable

    Hello all,
    dtable command is a nice addition however I am wondering if there is a way to have only percentages (no frequency). Please see my code and table below.

    dtable i.var1 i.var2 i.var3, by(industry2, nototals) export(table1.xlsx, replace)

    industry2
    1 2 3

    N (%) 269 (65.9%) 97 (23.8%) 42 (10.3%)

    i.var1
    1 9 (3.3%) 3 (3.1%) 3 (7.1%)
    2 (removed)
    3 (removed)


    i.var2
    1 4 (1.5%) 2 (2.1%) 0 (0.0%)
    2 (removed)
    3 (removed)

    i.var3
    1 41 (15.2%) 14 (14.4%) 6 (14.3%)
    2 (removed)
    3 (removed)

    ***********
    I would like to have:
    industry2
    1 2 3

    % 65.9% 23.8% 10.3%

    i.var1
    1 3.3% 3.1% 7.1%
    2 (removed)
    3 (removed)


    i.var2
    1 1.5% 2.1% 0.0%
    2 (removed)
    3 (removed)

    i.var3
    1 15.2% 14.4% 14.3%
    2 (removed)
    3 (removed)

  • #2
    Code:
    sysuse auto, clear
    dtable i.rep78, by(foreign)
    collect style cell foreign#result[percent fvpercent], nformat("%4.1f") sformat("%s")
    collect composite define mycol = percent fvpercent
    collect layout (var) (foreign#result[mycol])
    Res.:

    Code:
    . dtable i.rep78, by(foreign)
    
    ----------------------------------------------------
                                   Car origin           
                        Domestic    Foreign     Total   
    ----------------------------------------------------
    N                  52 (70.3%) 22 (29.7%) 74 (100.0%)
    Repair record 1978                                  
      1                  2 (4.2%)   0 (0.0%)    2 (2.9%)
      2                 8 (16.7%)   0 (0.0%)   8 (11.6%)
      3                27 (56.2%)  3 (14.3%)  30 (43.5%)
      4                 9 (18.8%)  9 (42.9%)  18 (26.1%)
      5                  2 (4.2%)  9 (42.9%)  11 (15.9%)
    ----------------------------------------------------
    
    
    . collect layout (var) (foreign#result[mycol])
    
    Collection: DTable
          Rows: var
       Columns: foreign#result[mycol]
       Table 1: 7 x 3
    
    -----------------------------------------
                             Car origin      
                       Domestic Foreign Total
    -----------------------------------------
    N                      70.3    29.7 100.0
    Repair record 1978                       
      1                     4.2     0.0   2.9
      2                    16.7     0.0  11.6
      3                    56.2    14.3  43.5
      4                    18.8    42.9  26.1
      5                     4.2    42.9  15.9
    -----------------------------------------
    
    .

    Comment


    • #3
      Great, it worked!
      Thank you so much!!

      Comment


      • #4
        Andrew provides the most general process/solution for working with
        an existing collection where you want to select a subset of the
        collected results.

        dtable has options that usually allow you to get your final table
        in a single command. In addition, these options are part of the
        dtable style, so you can save them to a file that you can reuse.

        Here is Andrew's example reworked to use these options (highlighted in blue).
        Code:
        sysuse auto, clear
        dtable i.rep78, ///
            by(foreign) ///
            sample(, statistic(percent)) ///
            factor(, statistic(fvpercent)) ///
            nformat(%4.1f percent fvpercent) ///
            sformat("%s" percent fvpercent)
        Here is the log from running the above.
        Code:
        . sysuse auto, clear
        (1978 automobile data)
        
        . dtable i.rep78, ///
        >         by(foreign) ///
        >         sample(, statistic(percent)) ///
        >         factor(, statistic(fvpercent)) ///
        >         nformat(%4.1f percent fvpercent) ///
        >         sformat("%s" percent fvpercent)
        
        -----------------------------------------
                                 Car origin
                           Domestic Foreign Total
        -----------------------------------------
        N                      70.3    29.7 100.0
        Repair record 1978
          1                     4.2     0.0   2.9
          2                    16.7     0.0  11.6
          3                    56.2    14.3  43.5
          4                    18.8    42.9  26.1
          5                     4.2    42.9  15.9
        -----------------------------------------
        Now let's save the styles in the current collection to a file.
        Code:
        . collect style save my-pct-dtable, replace
        (style from DTable saved to file my-pct-dtable.stjson)
        Now we can reference this file with option style() to reproduce the
        above table without having to retype all those options.
        Code:
        . dtable i.rep78, by(foreign) style(my-pct-dtable)
        
        -----------------------------------------
                                 Car origin
                           Domestic Foreign Total
        -----------------------------------------
        N                      70.3    29.7 100.0
        Repair record 1978
          1                     4.2     0.0   2.9
          2                    16.7     0.0  11.6
          3                    56.2    14.3  43.5
          4                    18.8    42.9  26.1
          5                     4.2    42.9  15.9
        -----------------------------------------
        See the Remarks and examples section titled Save your style choices for next time in [R] dtable for more details about dtable styles.

        Comment

        Working...
        X