Announcement

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

  • Calculating percentages within groups

    id groups variable%
    1 1 50
    1 1 50
    1 2 25
    1 3 23
    2 2 100
    2 2 100
    2 2 100
    3 1 100

    Hi,

    I have dataset having more than 100k observations. I would like to generate a variable that would allow me calculate the percentages of the number of each "groups" within "id".

    For example, I would like to generate "variable%" as a percentage of group values within groups i.e. 50% values in id=1 are spanned over group 1, 25% for group 2 and 3 (and so on for every id)

    I have tried egen command as: bysort id : egen total_id = count(id) - which allows me to calculate the totals of each id. But I am not able to figure out how to calculate percentages within groups. Any help in this regard would help a lot.







  • #2
    Could anyone able to help with this? I am completely stuck with this. I have tried to use egen command with serval variations (directly computing proportions, count totals and dividing with each other) but it does not solve within groups problem.

    Comment


    • #3
      You may wish to take a look at this FAQ.
      Best regards,

      Marcos

      Comment


      • #4
        Dear Abbas,
        You should read the post http://www.stata.com/support/faqs/da...ary-variables/

        Comment


        • #5
          Abbas:

          In #2 you bumped the thread after 42 minutes. For the future please follow advice on bumping in http://www.statalist.org/forums/help#adviceextras #1 .

          Otherwise, and noting that 23 is a typo, consider

          clear
          Code:
          input id    groups    wanted
          1    1    50
          1    1    50
          1    2    25
          1    3    25
          2    2    100
          2    2    100
          2    2    100
          3    1    100
          end 
          
          bysort id group: gen prop = _N 
          by id: replace prop = 100 * prop/_N 
          
          list, sepby(id) 
          
               +-----------------------------+
               | id   groups   wanted   prop |
               |-----------------------------|
            1. |  1        1       50     50 |
            2. |  1        1       50     50 |
            3. |  1        2       25     25 |
            4. |  1        3       25     25 |
               |-----------------------------|
            5. |  2        2      100    100 |
            6. |  2        2      100    100 |
            7. |  2        2      100    100 |
               |-----------------------------|
            8. |  3        1      100    100 |
               +-----------------------------+

          Comment

          Working...
          X