Announcement

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

  • Table: cumulative sum of percentages

    Hi,

    So, I'm trying to build a basic table, using ‘table’, with an age group, number people within that age group, percent of total and cumulative percent:
    Age group Sum of age group Percent of total Cumulative percentage
    0-17 år 5 16.67 16.67
    18-30 år 21 70 86.67
    30+ år 4 13.33 100
    It's the cumulative percent that baffles me... I can make it this far, but that's about it:

    Click image for larger version

Name:	table in stata.png
Views:	3
Size:	169.2 KB
ID:	1743936

    This is how the data is organised:

    Click image for larger version

Name:	Screenshot 2024-02-20 at 17.02.36.png
Views:	2
Size:	76.6 KB
ID:	1743938


    Such a basic question, but any ideas/solutions would be great, thanks!

    Anders


    Attached Files

  • #2
    table does not have a statistic() that computes the
    cumulative percentage. You will need to compute it yourself, then use
    collect get to get those values in your table collection.

    Here is an example using the auto data. In the following I use
    rep78 in place of your age_group, and mpg in place
    of your Antal variables.
    Code:
    sysuse auto
    
    * compute cumulative percent
    tabulate rep78 [fw=mpg], matcell(k)
    * k contains the weighted cell counts
    matrix list k
    * perc contains the correpsonding cell percentages
    matrix perc = 100*k/r(N)
    matrix list perc
    * cumul contains the cumulative cell percentages
    mata: st_matrix("cumul", runningsum(st_matrix("perc")))
    matrix list cumul
    * stripe the rows using FV notation
    fvexpand bn.rep78
    matrix rowname cumul = `r(varlist)'
    matrix list cumul
    
    * build your table
    table (rep78), stat(sum mpg) stat(percent mpg)
    * add matrix cumul to the collection;
    * transpose cumul, -collect- automatically attaches factor variables in
    * -colname- as their own dimension when tagging elements of a matrix
    collect get cumul=(matrix(cumul)')
    
    * label and styles
    collect label levels result cumul "Cumulative %", modify
    collect style cell result[cumul], nformat(%6.2f)
    
    collect layout
    Here is the resulting table.
    Code:
    ----------------------------------------------------
                       |  Total   Percent   Cumulative %
    -------------------+--------------------------------
    Repair record 1978 |
      1                |     42      2.86           2.86
      2                |    153     10.42          13.27
      3                |    583     39.69          52.96
      4                |    390     26.55          79.51
      5                |    301     20.49         100.00
      Total            |  1,469    100.00
    ----------------------------------------------------

    Comment


    • #3
      Fantastic, thanks Jeff!

      Comment


      • #4
        See also this FAQ from 2002.

        FAQ . . . . . . . . . . . . . . . . . . Tabulating cumulative frequencies
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
        9/02 How do I tabulate cumulative frequencies?
        http://www.stata.com/support/faqs/data-management/
        tabulating-cumulative-frequencies/

        Comment

        Working...
        X