Announcement

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

  • Mary Smith
    started a topic Display zero percentages using table/collect

    Display zero percentages using table/collect

    Hello,
    I'm a bit lost trying to figure out how to display zeros for factor variable percentages using the table and collect commands. I am using Stata 18.5 on Windows.

    Below is a simplified example of what I am trying to accomplish.

    Code:
    sysuse nlsw88
    
    table hours married collgrad // observse that there are zero observations of college graduates working 80 hours
    
    table collgrad if hours == 80, statistic(fvpercent married) // only shows percentages for non-college graduates with no row for college graduates at all
    the resulting output looks like this:
    Code:
    --------------------------------------
                       |       Married    
                       |  Single   Married
    -------------------+------------------
    College graduate   |                  
      Not college grad |   50.00     50.00
      Total            |   50.00     50.00
    --------------------------------------
    But I want it to look more like this manually-constructed example:

    Code:
    --------------------------------------
                       |       Married    
                       |  Single   Married
    -------------------+------------------
    College graduate   |                  
      Not college grad |   50.00     50.00
      College grad     |    0.00    0.00
      Total            |   50.00     50.00
    --------------------------------------

    The table looks the way I want it to when only one level of a variable has a zero:

    Code:
    . table collgrad if hours == 30, statistic(fvpercent married) 
    
    --------------------------------------
                       |       Married    
                       |  Single   Married
    -------------------+------------------
    College graduate   |                  
      Not college grad |   31.88     68.12
      College grad     |    0.00    100.00
      Total            |   28.21     71.79
    --------------------------------------
    So far I have tried this, but it didn't change the resulting table:

    Code:
    sysuse nlsw88
    table collgrad if hours == 80, statistic(fvpercent married) 
    collect style cell result, empty(0)
    collect preview
    
    --------------------------------------
                       |       Married    
                       |  Single   Married
    -------------------+------------------
    College graduate   |                  
      Not college grad |   50.00     50.00
      Total            |   50.00     50.00
    --------------------------------------

    This task is part of a larger block of code that uses loops to construct many tables, only some of which have zeros for some factor variable levels. Any potential solutions that require me to identify each cell/row with no observations and insert zeros manually would likely not be practical for my needs.

    Any ideas are appreciated!

  • Mary Smith
    replied
    This is helpful, thank you! I'll see if I can re-work this solution to fit my dataset.

    Leave a comment:


  • Andrew Musau
    replied
    Official tabulation commands do not handle absent categories because you do not know what is absent given that you are restricting the sample using the -if- qualifier. Even with community-contributed commands such as fre from SSC, there needs to be user input that specifies what is absent, either by explicitly including the list of levels of the variable or through a specification of the value labels. That means more work for you. In this case, before running the table command, you can find out which levels are missing and reconstruct the table using collect.

    Code:
    sysuse nlsw88, clear
    collect clear
    qui levelsof collgrad if hours == 80, local(levels)
    table collgrad if hours == 80, statistic(fvpercent married)
    if wordcount("`levels'")==1{
        collect get fvpercent=0, tags(collgrad[`=abs(`levels'-1)'] var[0.married])
        collect get fvpercent=0, tags(collgrad[`=abs(`levels'-1)'] var[1.married])
    }
    collect layout (collgrad) (result#var)
    Res.:

    Code:
    . collect layout (collgrad) (result#var)
    
    Collection: Table
          Rows: collgrad
       Columns: result#var
       Table 1: 4 x 2
    
    --------------------------------------
                       |       Married    
                       |  Single   Married
    -------------------+------------------
    College graduate   |                  
      Not college grad |   50.00     50.00
      College grad     |    0.00      0.00
      Total            |   50.00     50.00
    --------------------------------------
    This is the simplest case with a binary 0/1 indicator. More levels imply more conditions to fill in the missing rows.

    Leave a comment:

Working...
X