Announcement

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

  • collect is not following style cell declaration of nformat()

    I am running into two problems:
    1. I cannot use a binary variable for counting in the table. It seems to have to be a float, e.g. blood pressure or BMI
    I tried to find a solution in "STATA CUSTOMIZABLE TABLES AND COLLECTED RESULTS REFERENCE MANUAL RELEASE 18", but with no luck. .

    2. I cannot control number of decimals in percentages and add the "%" to that column estimates

    Something is not working with collect and the formatting of cells. I am working on version 19.5 with all updates implemented.
    I can do the same with a combination of collapse or simple counting and creating manual arrays with putdocx, but creating a targeted collection seems the optimal strategy from now onwards. I worked with Stata since about version 5 and have written many ado's, e.g. partgam.ado and venndiag.ado, so it should not be a lack of Stata practice.

    Let us solve issue 2 first - most likely simpler than #1.

    2: I followed the worked example in:
    https://blog.stata.com/2021/06/24/cu...lassic-table-1
    and made a simple table with two variables sex (n - %) and age (mean - SD). As you can see here:

    but notice number of decimals on percentages, e.g. 43.69874
    The percentages in column 2 and 4 are not following the "nformat" of 4.2f
    ----------------------------------------------
    Hypertension
    No Yes
    ----------------------------------------------
    Sex
    Male 2611 43.69874 2304 52.65082
    Female 3364 56.30126 2072 47.34918

    Age (years) 42 (16.8) 55 (14.9)
    ----------------------------------------------

    Can anyone suggest a solution ?

    cheers
    Jens Lauritsen, prof.,M.d.
    University of Southern Denmark

    code used is shown in full below:

    * version 17 // just to see if version was the problem.
    webuse nhanes2l
    * (Second National Health and Nutrition Examination Survey)

    cls
    collect clear
    table (var) (highbp), ///
    statistic(fvfrequency sex ) ///
    statistic(fvpercent sex) ///
    statistic(mean age) ///
    statistic(sd age) ///
    nototal

    * reorder to get only counts and percentages plus mean age/SD
    collect recode result fvfrequency = column1 ///
    fvpercent = column2 ///
    mean = column1 ///
    sd = column2 ///

    collect layout (var) (highbp#result[column1 column2])

    set dp period
    * numeric format of cells:
    collect style cell var[age]#result[column2], nformat(%4.1f)
    collect style cell var[sex age hlthstat]#result[column1], nformat(%6.0f)

    * add () to age and % to percentages of sex:
    collect style cell var[age]#result[column2], sformat("(%s)")

    /*
    I gave up on using
    collect style cell var[sex]#result[column2], sformat("(%s)")
    */

    collect style cell var[sex]#result[column2], nformat(%4.0f)

    collect preview

    * final touch:
    collect style cell border_block, border(right, pattern(nil)) // borders
    collect label dim highbp "Hypertension", modify // variable label
    collect style row stack, nobinder spacer // stack to avoid "sex="
    collect label levels highbp 0 "No" 1 "Yes" // explanation
    collect style header result, level(hide) // hide "column1 ....."
    collect preview
    Last edited by jens lauritsen; 13 Aug 2026, 06:31. Reason: spelling error

  • #2
    Originally posted by jens lauritsen View Post
    I am running into two problems:
    As you can see here:

    but notice number of decimals on percentages, e.g. 43.69874
    The percentages in column 2 and 4 are not following the "nformat" of 4.2f
    ----------------------------------------------
    Hypertension
    No Yes
    ----------------------------------------------
    Sex
    Male 2611 43.69874 2304 52.65082
    Female 3364 56.30126 2072 47.34918

    Age (years) 42 (16.8) 55 (14.9)
    ----------------------------------------------

    Can anyone suggest a solution ?
    I cannot see that you are referencing these columns. Always use collect layout and collect levelsof to view the dimensions of your table and levels of these dimensions to know how to reference specific rows, columns or cells.

    Code:
    help collect layout
    Code:
    help collect levelsof
    The following achieves what you want:

    Code:
    * version 17 // just to see if version was the problem.
    webuse nhanes2l, clear
    * (Second National Health and Nutrition Examination Survey)
    
    cls
    collect clear
    table (var) (highbp), ///
    statistic(fvfrequency sex ) ///
    statistic(fvpercent sex) ///
    statistic(mean age) ///
    statistic(sd age) ///
    nototal
    
    * reorder to get only counts and percentages plus mean age/SD
    collect recode result fvfrequency = column1 ///
    fvpercent = column2 ///
    mean = column1 ///
    sd = column2 ///
    
    collect layout (var) (highbp#result[column1 column2])
    
    set dp period
    * numeric format of cells:
    collect style cell var[age]#result[column2], nformat(%4.1f)
    collect style cell var[sex age hlthstat]#result[column1], nformat(%6.0f)
    
    * add () to age and % to percentages of sex:
    collect style cell var[age]#result[column2], sformat("(%s)")
    
    /*
    I gave up on using
    collect style cell var[sex]#result[column2], sformat("(%s)")
    */
    
    collect style cell var[sex]#result[column2], nformat(%4.0f)
    
    collect preview
    
    * final touch:
    collect style cell border_block, border(right, pattern(nil)) // borders
    collect label dim highbp "Hypertension", modify // variable label
    collect style row stack, nobinder spacer // stack to avoid "sex="
    collect label levels highbp 0 "No" 1 "Yes" // explanation
    collect style header result, level(hide) // hide "column1 ....."
    collect layout
    collect levelsof var
    collect style cell var[1.sex 2.sex]#highbp[]#result[column2], nformat(%4.2f)
    collect preview
    Res.:

    Code:
    . collect layout
    
    Collection: Table
          Rows: var
       Columns: highbp#result[column1 column2]
       Table 1: 5 x 4
    
    ----------------------------------------------
                            Hypertension          
                        No               Yes      
    ----------------------------------------------
    Sex                                          
      Male       2611   43.69874   2304   52.65082
      Female     3364   56.30126   2072   47.34918
                                                  
    Age (years)    42     (16.8)     55     (14.9)
    ----------------------------------------------
    
    . collect levelsof var
    
    Collection: Table
     Dimension: var
        Levels: 1.sex 2.sex age hlthstat sex
    
    . collect style cell var[1.sex 2.sex]#highbp[]#result[column2], nformat(%
    > 4.2f)
    
    . collect preview
    
    ------------------------------------------
                          Hypertension        
                       No             Yes    
    ------------------------------------------
    Sex                                      
      Male       2611    43.70   2304    52.65
      Female     3364    56.30   2072    47.35
                                              
    Age (years)    42   (16.8)     55   (14.9)
    ------------------------------------------

    Comment

    Working...
    X