Announcement

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

  • calculating percentages by household id

    Hello,

    I want to create a variable that shows the % of increase in care by household. Below, you can see the data. For the simplicity I colored the households and I only showed the child level increase of help and parent level increase of help. What I want is to find parent level increased help divided by the total number of increased help within that household. I am not sure how to do this simple calculation within the household. That would be really nice if anyone can help me.

    mergeid parent_level_increased_help) % increase help (variable I want to construct)
    "AT-001881-02" 1 50%
    "AT-001881-02" 1 50%

    "AT-002965-02" 0 0%
    "AT-002965-02" 0 .
    "AT-002965-02" 0 .
    "AT-002965-02" 0

    "AT-020460-02" 0
    "AT-020460-02" 0
    "AT-020460-02" 0
    "AT-020460-02" 0

    "AT-020975-02" 0
    "AT-020975-02" 0
    "AT-020975-02" 0

    "AT-034810-02" 1
    "AT-034810-02" 1

    "AT-044265-02" 0
    "AT-044265-02" 0

    "AT-046816-02" 2
    "AT-046816-02" 2


    Thanks in advance!




  • #2
    First use egen to calculate the totals by household (will be 2 for AT-001881-02) then use generate to find the shares in each row.

    Comment


    • #3

      This makes the helpful suggestion of Sergiy Radyakin a little more concrete.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str14 mergeid byte parent_level
      "AT-001881-02" 1
      "AT-001881-02" 1
      "AT-002965-02" 0
      "AT-002965-02" 0
      "AT-002965-02" 0
      "AT-002965-02" 0
      "AT-020460-02" 0
      "AT-020460-02" 0
      "AT-020460-02" 0
      "AT-020460-02" 0
      "AT-020975-02" 0
      "AT-020975-02" 0
      "AT-020975-02" 0
      "AT-034810-02" 1
      "AT-034810-02" 1
      "AT-044265-02" 0
      "AT-044265-02" 0
      "AT-046816-02" 2
      "AT-046816-02" 2
      end
      
      egen wanted = total(parent_level), by(mergeid)
      replace wanted = cond(wanted == 0, 0, 100 * parent_level / wanted)
      
      list
      
           +----------------------------------+
           |      mergeid   parent~l   wanted |
           |----------------------------------|
        1. | AT-001881-02          1       50 |
        2. | AT-001881-02          1       50 |
        3. | AT-002965-02          0        0 |
        4. | AT-002965-02          0        0 |
        5. | AT-002965-02          0        0 |
           |----------------------------------|
        6. | AT-002965-02          0        0 |
        7. | AT-020460-02          0        0 |
        8. | AT-020460-02          0        0 |
        9. | AT-020460-02          0        0 |
       10. | AT-020460-02          0        0 |
           |----------------------------------|
       11. | AT-020975-02          0        0 |
       12. | AT-020975-02          0        0 |
       13. | AT-020975-02          0        0 |
       14. | AT-034810-02          1       50 |
       15. | AT-034810-02          1       50 |
           |----------------------------------|
       16. | AT-044265-02          0        0 |
       17. | AT-044265-02          0        0 |
       18. | AT-046816-02          2       50 |
       19. | AT-046816-02          2       50 |
           +----------------------------------+
      
      .

      Comment

      Working...
      X