Announcement

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

  • Assign unique IDs to blocks of repeated values within groups?

    Hi everyone,

    I'm trying to assign a unique ID to blocks of repeated values within a group. For instance, I have a dataset like this:

    Person Activity
    A B
    A B
    A B
    A C
    A B
    A B

    What I desire is a ID that differentiate the group of "Bs" within each Person as if they were not the same activity. Something like this:

    Person Activity Unique ID
    A B 1
    A B 1
    A B 1
    A C 2
    A B 3
    A B 3


    Anyone has suggestions?

    Thank you SO MUCH!

  • #2
    Welcome to the forum. I think you want:

    Code:
    egen unique_id = group(person activity)
    Edit, oh, looks like I misunderstood.
    Last edited by Daniel Schaefer; 13 Aug 2023, 15:08.

    Comment


    • #3
      Hi! Thank you for your answer. Unfortunately, this command does not do what I want. I want to give different IDs to "B" when they are not repeated in order. So, using the above command I will obtain always ID "1" for B, however I want to assign B two different IDs because they are not repeated in order. Like this:

      Activity ID
      B 1
      B 1
      B 1
      C 2
      C 2
      C 2
      B 3
      B 3

      Thank you again!

      Comment


      • #4
        So something like this maybe:

        Code:
        clear
        input str20(person activity expected_result)
        A B 1
        A B 1
        A B 1
        A C 2
        A B 3
        A B 3
        B C 4
        B C 4
        B C 4
        B D 5
        B D 5
        end
        
        local previous_person = ""
        local previous_activity = ""
        local id = 0
        gen result = .
        forv row = 1/`=_N'{
            local current_person = person[`row']
            local current_activity = activity[`row']
            if ("`current_person'" == "`previous_person'" & "`current_activity'" == "`previous_activity'"){
                replace result = `id' in `row'
            }
            else{
                local id = `id' + 1
                replace result = `id' in `row'
            }
            local previous_person = "`current_person'"
            local previous_activity = "`current_activity'"
        }
        
        list
        Code:
        . list
        
             +---------------------------------------+
             | person   activity   expect~t   result |
             |---------------------------------------|
          1. |      A          B          1        1 |
          2. |      A          B          1        1 |
          3. |      A          B          1        1 |
          4. |      A          C          2        2 |
          5. |      A          B          3        3 |
             |---------------------------------------|
          6. |      A          B          3        3 |
          7. |      B          C          4        4 |
          8. |      B          C          4        4 |
          9. |      B          C          4        4 |
         10. |      B          D          5        5 |
             |---------------------------------------|
         11. |      B          D          5        5 |
             +---------------------------------------+

        Comment


        • #5
          Here is a better "vectorized" solution:

          Code:
          clear
          input str20(person activity expected_result)
          A B 1
          A B 1
          A B 1
          A C 2
          A B 3
          A B 3
          B C 4
          B C 4
          B C 4
          B D 5
          B D 5
          end
          
          gen transition = person != person[_n-1] | activity != activity[_n-1]
          gen result = sum(transition)
          drop transition
          list
          Code:
          . list
          
               +---------------------------------------+
               | person   activity   expect~t   result |
               |---------------------------------------|
            1. |      A          B          1        1 |
            2. |      A          B          1        1 |
            3. |      A          B          1        1 |
            4. |      A          C          2        2 |
            5. |      A          B          3        3 |
               |---------------------------------------|
            6. |      A          B          3        3 |
            7. |      B          C          4        4 |
            8. |      B          C          4        4 |
            9. |      B          C          4        4 |
           10. |      B          D          5        5 |
               |---------------------------------------|
           11. |      B          D          5        5 |
               +---------------------------------------+

          Comment


          • #6
            You do not need to loop.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str20(person activity)
            "A" "B"
            "A" "B"
            "A" "B"
            "A" "C"
            "A" "B"
            "A" "B"
            "B" "C"
            "B" "C"
            "B" "C"
            "B" "D"
            "B" "D"
            end
            
            gen long order=_n
            bys person (order): gen wanted= sum(activity!=activity[_n-1])
            Res.:

            Code:
            . l, sepby(person)
            
                 +------------------------------------+
                 | person   activity   order   wanted |
                 |------------------------------------|
              1. |      A          B       1        1 |
              2. |      A          B       2        1 |
              3. |      A          B       3        1 |
              4. |      A          C       4        2 |
              5. |      A          B       5        3 |
              6. |      A          B       6        3 |
                 |------------------------------------|
              7. |      B          C       7        1 |
              8. |      B          C       8        1 |
              9. |      B          C       9        1 |
             10. |      B          D      10        2 |
             11. |      B          D      11        2 |
                 +------------------------------------+
            
            .

            Comment


            • #7
              It worked! Thanks so much for your help.

              Comment


              • #8
                Thanks Andrew, that looks right.

                Comment


                • #9
                  Thanks so much both of you!

                  Comment

                  Working...
                  X