Announcement

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

  • Formatting percentages in table collect

    Hi Statalist, I'm writing with a question that seems to be uncomfortably simple, which I just can't sort out. I'm hoping someone can help me with an easy fix. The gist of the issue is that I would like to use table collect to present frequency and percent values, but after doing some table collect formatting, I don't seem to be able to get what I want. Here is a code example:


    Code:
    webuse lifeexp, clear
    
    drop if safewater < 80 //this is just so that I can produce tables for this example that do not go on forever and ever
    
    table (var) region, stat(fvfrequency safewater) stat(fvpercent safewater) nformat(%9.0fc fvfrequency) nformat(%9.1fc fvpercent)
    Here's the primary result, which has the number formatting I want, but has stacked frequency and percent in rows when I want them in columns:
    Click image for larger version

Name:	Capture1.PNG
Views:	1
Size:	31.5 KB
ID:	1781891




    OK, fine. So then I add some formatting as follows:
    Code:
    collect style row stack, nobinder spacer
    collect recode result fvfrequency=col1 fvpercent=col2
    collect layout (var) (region#result[col1 col2])
    And now I have the correct column formatting, but percentages are back to having >1 decimal place:
    Click image for larger version

Name:	Capture2.PNG
Views:	1
Size:	13.6 KB
ID:	1781892




    Additional attempts to change the cell style don't appear to do anything:
    Code:
    collect style cell var[safewater]#result[col1], nformat(%6.0fc)
    collect style cell var[safewater]#result[col2], nformat(%6.1f) sformat("%s%%")
    collect preview
    Click image for larger version

Name:	Capture3.PNG
Views:	1
Size:	18.0 KB
ID:	1781894




    I'm sure I'm just doing something wrong from a syntax perspective, but cannot for the life of me figure out what it is. Could someone assist if you spot my error?
    Last edited by Maria Sundaram; 16 Sep 2025, 11:25.
Working...
X