Announcement

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

  • Ranking and generating variables for subgroups within groups

    Hello everyone. Would love some help on the following test data set!

    How do I generate a variable employ1 which gives me for every state, whichever party has the maximum employment? And then a variable employ2 which gives me for every state, whichever party has the second highest employment? I made a small dataset to illustrate the example, my actual data set is much larger with a lot more states and parties.

    For example, for state AP, employ1 would be BLUE and employ2 would be PINK, as in state AP, the BLUE party employs the most people and the PINK party employs the second highest number of people.
    Similarly for state HP, employ1 would be PINK and employ2 would be RED.

    Thanks in advance!
    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	8.2 KB
ID:	1614595

  • #2
    Code:
    bysort STATE (totalemploy): gen employ1 = PARTY[_N]
    bysort STATE (totalemploy): gen employ2 = PARTY[_N-1]
    Note this assumes no ties. If ties are an issue, please post back with rules for selecting a winner for both employ1 and employ2

    Comment


    • #3
      Originally posted by Ali Atia View Post
      Code:
      bysort STATE (totalemploy): gen employ1 = PARTY[_N]
      bysort STATE (totalemploy): gen employ2 = PARTY[_N-1]
      Note this assumes no ties. If ties are an issue, please post back with rules for selecting a winner for both employ1 and employ2
      Thank you. This worked perfectly.

      Comment

      Working...
      X