Announcement

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

  • Generating a new variable: the share of female candidates per political party per election year

    Dear Stata Forum Users,
    I have what will be a basic question for advanced Stata users, for which I apologize.
    If anyone could help with the correct syntax, I would be very grateful.
    I am trying to create a variable that represents the share of female candidates per political party per election year.
    The coding is as follows:
    sex 0,1 (0=male, 1=female);
    seito_r 1-53 (nominal variable coded 1 - 53 each category is a political party)
    year is labeled 1996-2024 for each election year, coded 1-10 (10 elections during this time period)
    I am using Stata/SE 18.0
    Best regards,
    Gill

    [CODE]
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte sex int year long seito_r
    0 1996 30
    0 1996 46
    0 1996 35
    1 1996 10
    1 1996 24
    0 1996 13
    0 1996 38
    0 1996 30
    0 1996 46
    0 1996 35
    0 1996 10
    1 1996 48
    0 1996 24
    0 1996 38
    0 1996 13
    0 1996 30
    0 1996 46
    0 1996 35
    1 1996 10
    1 1996 13
    0 1996 24
    0 1996 38
    0 1996 30
    0 1996 46
    1 1996 10
    0 1996 35
    0 1996 13
    0 1996 24
    0 1996 35
    0 1996 46
    0 1996 30
    0 1996 10
    0 1996 38
    0 1996 13
    0 1996 24
    0 1996 30
    0 1996 46
    0 1996 35
    1 1996 10
    0 1996 13
    1 1996 24
    0 1996 38
    0 1996 22



  • #2
    You can exploit the fact that the mean of a 0/1 variable is a proportion.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte sex int year long seito_r
    0 1996 30
    0 1996 46
    0 1996 35
    1 1996 10
    1 1996 24
    0 1996 13
    0 1996 38
    0 1996 30
    0 1996 46
    0 1996 35
    0 1996 10
    1 1996 48
    0 1996 24
    0 1996 38
    0 1996 13
    0 1996 30
    0 1996 46
    0 1996 35
    1 1996 10
    1 1996 13
    0 1996 24
    0 1996 38
    0 1996 30
    0 1996 46
    1 1996 10
    0 1996 35
    0 1996 13
    0 1996 24
    0 1996 35
    0 1996 46
    0 1996 30
    0 1996 10
    0 1996 38
    0 1996 13
    0 1996 24
    0 1996 30
    0 1996 46
    0 1996 35
    1 1996 10
    0 1996 13
    1 1996 24
    0 1996 38
    0 1996 22
    end
    
    assert inlist(sex, 0, 1)
    bys seito_r year: egen wanted= mean(sex)
    Res.:

    Code:
    .  l, sepby(seito_r year)
    
         +---------------------------------+
         | sex   year   seito_r     wanted |
         |---------------------------------|
      1. |   1   1996        10   .6666667 |
      2. |   0   1996        10   .6666667 |
      3. |   1   1996        10   .6666667 |
      4. |   1   1996        10   .6666667 |
      5. |   1   1996        10   .6666667 |
      6. |   0   1996        10   .6666667 |
         |---------------------------------|
      7. |   0   1996        13   .1666667 |
      8. |   0   1996        13   .1666667 |
      9. |   0   1996        13   .1666667 |
     10. |   0   1996        13   .1666667 |
     11. |   0   1996        13   .1666667 |
     12. |   1   1996        13   .1666667 |
         |---------------------------------|
     13. |   0   1996        22          0 |
         |---------------------------------|
     14. |   0   1996        24   .3333333 |
     15. |   0   1996        24   .3333333 |
     16. |   1   1996        24   .3333333 |
     17. |   1   1996        24   .3333333 |
     18. |   0   1996        24   .3333333 |
     19. |   0   1996        24   .3333333 |
         |---------------------------------|
     20. |   0   1996        30          0 |
     21. |   0   1996        30          0 |
     22. |   0   1996        30          0 |
     23. |   0   1996        30          0 |
     24. |   0   1996        30          0 |
     25. |   0   1996        30          0 |
         |---------------------------------|
     26. |   0   1996        35          0 |
     27. |   0   1996        35          0 |
     28. |   0   1996        35          0 |
     29. |   0   1996        35          0 |
     30. |   0   1996        35          0 |
     31. |   0   1996        35          0 |
         |---------------------------------|
     32. |   0   1996        38          0 |
     33. |   0   1996        38          0 |
     34. |   0   1996        38          0 |
     35. |   0   1996        38          0 |
     36. |   0   1996        38          0 |
         |---------------------------------|
     37. |   0   1996        46          0 |
     38. |   0   1996        46          0 |
     39. |   0   1996        46          0 |
     40. |   0   1996        46          0 |
     41. |   0   1996        46          0 |
     42. |   0   1996        46          0 |
         |---------------------------------|
     43. |   1   1996        48          1 |
         +---------------------------------+
    Last edited by Andrew Musau; 18 Feb 2025, 00:49.

    Comment


    • #3
      a basic question for advanced Stata users, for which I apologize
      No need to apologise. The trick here should seem clear once pointed out, but that's the nature of every little trick.

      https://journals.sagepub.com/doi/pdf...867X1101100210 is perhaps more of the same kind of thing.

      Comment


      • #4
        A huge and heartfelt thank you. You might be astonished to find how much time I spent trying to figure this out.
        Thank you for the link. I will take a look now.
        I really appreciate your time and expertise.
        Sincerely,
        Gill

        Comment

        Working...
        X