Announcement

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

  • collect style cell & collect style row - appearance of borders

    Dear Stata Users,

    I've got a problem formatting various tables, which I generate with "table" / "collect layout" in Stata 18 (and add later to a word document with "putdocx collect").

    I've got several dummy variables in my dataset, indicating if a respondend mentioned a problem. The variables in the example indicate that the first problem was mentioned once (V401), the second problem was mentioned 8 times (V402), "don't know" was mentioned 4 times.

    I would like to seperate the last category, using "collect style cell". This works fine (example 1) until I add a blank line between the categories/variables/dimensions, which I would like to do for clarity. In this case, there are not 1 but 2 border lines (example 2).

    The problem also accurs in other tables if 2 or more variables with more than 2 categories are included.

    Can "collect style cell, border" and "collect style row stack, spacer" combinded somehow?

    Any ideas or workarounds would be appreciated. Thanks a lot!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    collect clear
    clear
    input byte(V401 V402 V403) float V033
    0 0 0 2
    0 1 0 2
    0 1 0 1
    0 1 0 2
    0 0 0 2
    0 0 0 1
    0 0 0 1
    0 0 0 1
    0 0 0 2
    0 0 1 2
    0 0 0 1
    0 1 0 2
    1 1 0 1
    0 1 0 1
    0 0 0 1
    0 0 1 1
    0 0 0 1
    0 0 0 2
    0 0 0 2
    0 1 0 1
    0 0 1 2
    0 0 1 1
    0 0 0 1
    0 1 0 1
    0 0 0 2
    0 0 0 1
    0 0 0 1
    0 0 0 1
    0 0 0 1
    0 0 0 2
    end
    
    label variable V033 "Region"
    label variable V401 "Problem#1"
    label variable V402 "Problem#2"
    label variable V403 "don't know"
    
    label values V033 V033
    label def V033 1 "1. West", modify
    label def V033 2 "2. East", modify
    
    
    foreach var of varlist V401 V402 V403 {
    
        qui table (`var'),                  ///      // distributions                          
        nototals                            ///  
        statistic(percent)                  ///
        name(table1) append                              
                       
        collect remap result = distribution                  
    }
    
    foreach var of varlist V401 V402 V403 {               
    
        qui table (`var') (V033),           ///      // crosstab
        nototals                            ///     
        statistic(percent, across(`var'))   ///
        name(table1)  append     
    }
            
    collect style header V401 V402 V403, level(hide)                 // hide level header
    
    * example 1
    
            collect style cell V403,       ///                      // seperate last category
            border(top, pattern(dotted))
    
    collect layout (V401[1] V402[1]    V403[1]) (distribution V033)      
    
    * example 2
    
            collect style row stack, spacer                     // add a blank line between row dimensions
    
    collect layout (V401[1] V402[1]    V403[1]) (distribution V033)

  • #2
    collect's layout, spacer, and border logic is not up to handling
    this situation.

    Here is one work-around, introduce a new result named
    space that you can use to inject spaces in the row specification.

    Code:
    * turn off spacers
    collect style row stack, nospacer
    * introduce new 'space' result
    collect get space=(" "), tags(distribution[proportion])
    * hide result 'space' in the header
    collect style header result[space], level(hide)
    * use 'result[space]' where you want to have a custom spacer
    collect layout (V401[1] result[space] V402[1] result[space] V403[1]) (distribution V033)
    Here is the resulting table.
    Code:
    ---------------------------------------------------------
               |       distribution              Region      
               |   percent   proportion    1. West    2. East
    -----------+---------------------------------------------
    Problem#1  |  3.333333                5.555556           
               |                                             
    Problem#2  |  26.66667                27.77778         25
               |                                             
    -----------+---------------------------------------------
    don't know |  13.33333                11.11111   16.66667
    ---------------------------------------------------------

    Comment


    • #3
      Dear Jeff Pitblado ,

      thank you very much for your response. Including a custom spacer might be pretty helpful in other contexts as well.

      I've looked at it and think that I can implement it pretty much. I can't use "collect style cell, empty("-‎")", but thats ok.

      Best, Simon

      Comment


      • #4
        Dear Jeff Pitblado,

        I hope I am not missing anything, but maybe the code may be slightly changed to get rid of the empty column "proportion".

        If I chose not "proportion" but the exitsing "percent" for the level of "distribution", the resulting table has blank lines and also an additional border line above V403, but no empty column.

        Thanks again!

        Code:
        * turn off spacers
        collect style row stack, nospacer
        
        * introduce new 'space' result
        collect get space=(" "), tags(distribution[percent])
        
        * hide result 'space' in the header
        collect style header result[space], level(hide)
        
        * use 'result[space]' where you want to have a custom spacer
        collect layout (V401[1] result[space] V402[1] result[space] V403[1]) (distribution V033)
        Here is the resulting table:

        Code:
        ------------------------------------------------
                   |  distribution          Region      
                   |       percent    1. West    2. East
        -----------+------------------------------------
        Problem#1  |      3.333333   5.555556           
                   |                                    
        Problem#2  |      26.66667   27.77778         25
                   |                                    
        -----------+------------------------------------
        don't know |      13.33333   11.11111   16.66667
        ------------------------------------------------

        Comment


        • #5
          Hi Simon,
          You are not missing anything. I've been working with percent and proportion so much, that I saw one but typed the other. I rushed my post, focusing on the row header spaces, I did not notice that I introduced a blank column in the example.

          You figured out the fix to my typo.

          Comment


          • #6
            Hi Jeff,

            ok, thank's for pointing out!
            I am glad it works.

            Best, Simon

            Comment

            Working...
            X