Announcement

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

  • Generating groups defined by multiple variables

    Hi Statalists,

    I am trying to generate an enumeration variable for groups which are defined by other variables.
    In the example below I likewise try to generate X which is distinct for every B sorted by A.

    Code:
    Code:
    A   B   X
    3   1   1
    3   1   1
    3   1   1
    3   1   1
    3   0   2
    3   0   2   
    3   0   2
    7   1   3
    7   1   3
    7   1   3
    7   1   3
    7   0   4
    7   0   4
    Thank You

  • #2
    It depends. If you just want a unique ID, you can do "egen X = group(A B)" but if you want to replicate "X" then, since B is sorted in descending order, you'd have to do "gsort A -B, gen(X)"

    Comment


    • #3
      Thank you very much Mauricio!

      Comment


      • #4
        There is a trick issue here.

        You decided to start the sequence with 1 for variable B, instead of 0. That can be confusing.

        Ignoring this aspect and just giving a sequence may be a solution.

        After all, there are levels of a categorical variable, and Stata will recognize which is which:

        Code:
        . egen float X2 = group(A B)
        
        . list
        
             +----------------+
             | A   B   X   X2 |
             |----------------|
          1. | 3   1   1    2 |
          2. | 3   1   1    2 |
          3. | 3   1   1    2 |
          4. | 3   1   1    2 |
          5. | 3   0   2    1 |
             |----------------|
          6. | 3   0   2    1 |
          7. | 3   0   2    1 |
          8. | 7   1   3    4 |
          9. | 7   1   3    4 |
         10. | 7   1   3    4 |
             |----------------|
         11. | 7   1   3    4 |
         12. | 7   0   4    3 |
         13. | 7   0   4    3 |
             +----------------+
        Last edited by Marcos Almeida; 17 Jan 2018, 08:15.
        Best regards,

        Marcos

        Comment

        Working...
        X