Announcement

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

  • Exporting variable notes in table instead of variable labels

    I have some variable labels that are longer than 80 characters that I'd like to export to a table. I was wondering if there is a way to use notes, or any other method, as a workaround for the variable label character limit. In the below example I've attached both a label and note to the variable of interest, but I'd like for the output to show the variable note instead of the variable label, bypassing the truncation. The below code produces a table as pictured.

    I'm using Stata 18 and using the dtable, label, notes and putdocx commands. It would be helpful to get this working for all sorts of exports with other commands, but I'm not sure if that is a different issue entirely. Full transparency, this is a repost.

    Code:
    clear
    set seed 82
    set obs 1000
    g female = runiformint(0,1)
    label define female 0 "Male" 1 "Female"
    label values female female
    g cont = female + runiformint(1,7) + runiform() if runiform() > .05
    g cat = 1
    replace cat = 2 if cont > 2
    replace cat = 3 if cont > 5
    la var cont "Continuous variable"
    la var cat "This is a question with multiple answer choices, where the question is longer than 80 characters"
    notes cat : "This is a question with multiple answer choices, where the question is longer than 80 characters"
    
    dtable cont i.cat, by(female, tests)
    putdocx begin
    putdocx collect
    putdocx save mytable, replace
    
    
    ----------------------------------------------------------------------------------------------------------------------------------
                                                                                                           female                    
                                                                                          Male         Female         Total      Test
    ----------------------------------------------------------------------------------------------------------------------------------
    N                                                                                  501 (50.1%)   499 (49.9%) 1,000 (100.0%)      
    Continuous variable                                                              4.513 (2.067) 5.453 (2.024)  4.977 (2.098) <0.001
    This is a question with multiple answer choices, where the question is longer th                                                  
      1                                                                                 65 (13.0%)      0 (0.0%)      65 (6.5%) <0.001
      2                                                                                208 (41.5%)   206 (41.3%)    414 (41.4%)      
      3                                                                                228 (45.5%)   293 (58.7%)    521 (52.1%)      
    ----------------------------------------------------------------------------------------------------------------------------------
    Last edited by Matthew Wallace; 31 Jul 2023, 14:04.

  • #2
    Repeating the same question does not help. Refer to https://www.statalist.org/forums/help#adviceextras Advice #1.2. I do not have Stata 18 and therefore I am not familiar with dtable to help you. Exporting a note to putdocx is doable, but you seem to be asking how to replace a label with a note in a table that is programmed to export labels, which does not appear to be straightforward without rewriting the underlying code of the command. Have you considered creating the table with collect to change some of the defaults?

    Comment


    • #3
      I added in additional context that I thought was relevant enough for a bump, but I realize I should've bumped the same thread with that info instead of creating a new one.

      I've been trying to use both dtable and a table created through collect to no avail.

      Comment


      • #4
        Read through the documentation of collect. It allows you to build a table from scratch. Here is a sketch.

        Code:
        clear
        set seed 82
        set obs 1000
        g female = runiformint(0,1)
        label define female 0 "Male" 1 "Female"
        label values female female
        g cont = female + runiformint(1,7) + runiform() if runiform() > .05
        g cat = 1
        replace cat = 2 if cont > 2
        replace cat = 3 if cont > 5
        la var cont "Continuous variable"
        la var cat "This is a question with multiple answer choices, where the question is longer than 80 characters"
        notes cat : This is a question with multiple answer choices, where the question is longer than 80 characters
        
        collect clear
        collect get row1 = "xx.xx", tags(col[Male])
        collect get row1 = "xx.xx", tags(col[Female])
        collect get row2 = "yy.yy", tags(col[Male])
        collect get row2 = "yy.yy", tags(col[Female])
        collect get row3 = " ", tags(col[Male])
        collect get row3 = " ", tags(col[Female])
        collect get row4 = "zz.zz", tags(col[Male])
        collect get row4 = "zz.zz", tags(col[Female])
        collect get row5 = "zz.zz", tags(col[Male])
        collect get row5 = "zz.zz", tags(col[Female])
        collect get row6 = "zz.zz", tags(col[Male])
        collect get row6 = "zz.zz", tags(col[Female])
        collect layout (result) (col)
        collect label levels result row1 "N" row2 "Continuous variable" row3 `"`cat[note1]'"' row4 "1" row5 "2" row6 "3"  
        collect label dim col "Sex"
        collect style header col, title(label)
        collect style column, dups(center)
        collect style cell cell_type[corner], border(right, pattern(nil))
        collect style cell col#cell_type[column-header], border(bottom, pattern(single))
        collect style cell result, border(right, pattern(nil))
        collect title "Table 1. Some title"
        collect style title, smcl(input)
        collect preview
        Res.:

        Code:
        Table 1. Some title
        -------------------------------------------------------------------------------------------------------------
                                                                                                              Sex    
                                                                                                         ------------
                                                                                                          Male Female
        -------------------------------------------------------------------------------------------------------------
        N                                                                                                xx.xx  xx.xx
        Continuous variable                                                                              yy.yy  yy.yy
        This is a question with multiple answer choices, where the question is longer than 80 characters            
        1                                                                                                zz.zz  zz.zz
        2                                                                                                zz.zz  zz.zz
        3                                                                                                zz.zz  zz.zz
        -------------------------------------------------------------------------------------------------------------
        Last edited by Andrew Musau; 31 Jul 2023, 20:06.

        Comment


        • #5
          Thanks so much, Andrew. I did not know that collect label does not have the 80 character limit.

          Comment

          Working...
          X