Announcement

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

  • Collect Style Cell: Changing the style of a cell no longer works

    Hello,

    In the past, I have been able to change the style of the cell (line 43 in the code below), to change the numbering format and add a % sign next to the numbers in those cells. However, I have noticed that as of recent, this no longer works.

    The following code:

    Code:
    clear
    webuse nhanes2l
    tab highbp
    
    collect clear
    collect layout, clear
        
    * Obtain an initial table with necessary statistics                
    collect: table ///
        (var) ///
        (highbp), ///
            statistic(mean age) ///
                statistic(sd age) ///
                statistic(fvfrequency race) ///
                statistic(fvpercent race)
    
    * Recode the levels of the result dimension to allow combined placement of the statistic of interest later in the shared cells in the final table
    collect recode result ///
        fvfrequency = variable_measure ///
        fvpercent = variable_spread ///
        mean = variable_measure ///
        sd = variable_spread //
    
    *Add labels to the mentioned combined levels for better table readability
    collect label levels result variable_measure "Count / Mean" variable_spread "% / (SD)"  
    
    *Changing cell styles
    
        * continuous variables
            collect style cell ///
                var[age]#result[variable_measure] ///
                , nformat(%4.0fc)
            
            collect style cell ///
                var[age]#result[variable_spread] ///
                , nformat(%4.1fc) sformat("(%s)")
    
        * categorical variables
    
            collect style cell ///
                var[race]#result[variable_measure] ///
                , nformat(%4.0fc)
            
            collect style cell ///
                var[race]#result[variable_spread] ///
                , nformat(%4.0fc) sformat("%s%%") // THIS IS THE COMMAND THAT DOES NOT APPLY
    
    *Create the final table  
    collect layout ///
        (var) ///
        (highbp#result[variable_measure variable_spread])
    ... results in the following table:

    Code:
    Collection: Table
          Rows: var
       Columns: highbp#result[variable_measure variable_spread]
       Table 1: 4 x 6
    
    ------------------------------------------------------------------------------------------
                |                              High blood pressure                            
                |             0                         1                       Total        
                |  Count / Mean   % / (SD)   Count / Mean   % / (SD)   Count / Mean   % / (SD)
    ------------+-----------------------------------------------------------------------------
    Age (years) |            42     (16.8)             55     (14.9)             48     (17.2)
    Race=White  |          5317   88.98745           3748   85.64899           9065   87.57608
    Race=Black  |           545   9.121339            541   12.36289           1086   10.49174
    Race=Other  |           113   1.891213             87   1.988117            200    1.93218
    ------------------------------------------------------------------------------------------

    In above table, in the cells of the %/(SD) column of the Race variable, the numbers are not formatted as they should be by line 43 of the code above, which is the following line of code below:

    Code:
            collect style cell ///
                var[race]#result[variable_spread] ///
                , nformat(%4.0fc) sformat("%s%%")
    Can you advise as to what might be the reason? Is the code syntax incorrect (even though I do not get an error)?


    Thank you for your support!
    Last edited by Nathan Yu; 11 Oct 2024, 00:27.

  • #2
    The displayed variables are levels of the factor race. So they should be referenced as "i.race" or explictly (1.race 2.race 3.race) and not simply "race".

    Code:
    clear
    webuse nhanes2l
    tab highbp
    
    collect clear
    collect layout, clear
        
    * Obtain an initial table with necessary statistics                
    collect: table ///
        (var) ///
        (highbp), ///
            statistic(mean age) ///
                statistic(sd age) ///
                statistic(fvfrequency race) ///
                statistic(fvpercent race)
    
    * Recode the levels of the result dimension to allow combined placement of the statistic of interest later in the shared cells in the final table
    collect recode result ///
        fvfrequency = variable_measure ///
        fvpercent = variable_spread ///
        mean = variable_measure ///
        sd = variable_spread //
    
    *Add labels to the mentioned combined levels for better table readability
    collect label levels result variable_measure "Count / Mean" variable_spread "% / (SD)"  
    
    *Changing cell styles
    
        * continuous variables
            collect style cell ///
                var[age]#result[variable_measure] ///
                , nformat(%4.0fc)
            
            collect style cell ///
                var[age]#result[variable_spread] ///
                , nformat(%4.1fc) sformat("(%s)")
    
        * categorical variables
    
            collect style cell ///
                var[i.race]#result[variable_measure] ///
                , nformat(%4.0fc)
            
            collect style cell ///
                var[i.race]#result[variable_spread] ///
                , nformat(%4.0fc) sformat("%s%%") 
    
    *Create the final table  
    collect layout ///
        (var) ///
        (highbp#result[variable_measure variable_spread])
    Res.:

    Code:
    .     * categorical variables
    . 
    .         collect style cell ///
    >             var[i.race]#result[variable_measure] ///
    >             , nformat(%4.0fc)
    
    .         
    .         collect style cell ///
    >             var[i.race]#result[variable_spread] ///
    >             , nformat(%4.0fc) sformat("%s%%") 
    
    . 
    . *Create the final table  
    . collect layout ///
    >     (var) ///
    >     (highbp#result[variable_measure variable_spread])
    
    Collection: Table
          Rows: var
       Columns: highbp#result[variable_measure variable_spread]
       Table 1: 4 x 6
    
    ------------------------------------------------------------------------------------------
                |                              High blood pressure                            
                |             0                         1                       Total         
                |  Count / Mean   % / (SD)   Count / Mean   % / (SD)   Count / Mean   % / (SD)
    ------------+-----------------------------------------------------------------------------
    Age (years) |            42     (16.8)             55     (14.9)             48     (17.2)
    Race=White  |          5317        89%           3748        86%           9065        88%
    Race=Black  |           545         9%            541        12%           1086        10%
    Race=Other  |           113         2%             87         2%            200         2%
    ------------------------------------------------------------------------------------------

    Comment


    • #3
      Oh I see, thank you so much Andrew for this correction!

      Comment

      Working...
      X