Announcement

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

  • Variable equal to any previous value within group

    Hi all,

    I am trying to create a new variable that indicates whether a switch between organizations the focal firm works with was a switch back to a firm they have already previously worked with. I have added some sample data below. The assignee is the focal firm and the lawfirm is the firm they are working with. I have already created a variable "switchlaw" indicating whether a new observation indicates a change from the previous one. Now I would like to create a new variable taking the value one whenever a switch (so switchlaw==1) represents a switch back to a firm that the assignee has already worked with. So, for example, the new variable should take the value "0" in row two (lawfirm 2 has not been worked with before) but the value "1" in row four (lawfirm two had already been worked with in row two. For the fourth to last row (assignee two lawfirm one) the variable should take the value 0, however, since assignee two hasn't worked with lawfirm one, only assignee one has.

    I feel like a loop and using _N might be in order here but I couldn't quite figure it out by myself. Any help would be very much appreciated.

    Thanks everybody


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(assignee lawfirm) float switchlaw
    1 1 .
    1 2 1
    1 3 1
    1 2 1
    1 2 0
    1 2 0
    1 1 1
    1 2 1
    2 2 .
    2 4 1
    2 5 1
    2 1 1
    2 5 1
    2 4 1
    2 6 1
    end

  • #2


    Thanks for the clear data example. I am surprised not to see a time or sequence variable here. So, I created one. Otherwise, no loops needed: it's a problem that yields to by:.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(assignee lawfirm) float switchlaw
    1 1 .
    1 2 1
    1 3 1
    1 2 1
    1 2 0
    1 2 0
    1 1 1
    1 2 1
    2 2 .
    2 4 1
    2 5 1
    2 1 1
    2 5 1
    2 4 1
    2 6 1
    end
    
    sort assignee, stable 
    by assignee: gen seq = _n 
    bysort assignee lawfirm (seq) : gen previous = _n > 1 
    
    list , sepby(assignee lawfirm) 
    
         +------------------------------------------------+
         | assignee   lawfirm   switch~w   seq   previous |
         |------------------------------------------------|
      1. |        1         1          .     1          0 |
      2. |        1         1          1     7          1 |
         |------------------------------------------------|
      3. |        1         2          1     2          0 |
      4. |        1         2          1     4          1 |
      5. |        1         2          0     5          1 |
      6. |        1         2          0     6          1 |
      7. |        1         2          1     8          1 |
         |------------------------------------------------|
      8. |        1         3          1     3          0 |
         |------------------------------------------------|
      9. |        2         1          1     4          0 |
         |------------------------------------------------|
     10. |        2         2          .     1          0 |
         |------------------------------------------------|
     11. |        2         4          1     2          0 |
     12. |        2         4          1     6          1 |
         |------------------------------------------------|
     13. |        2         5          1     3          0 |
     14. |        2         5          1     5          1 |
         |------------------------------------------------|
     15. |        2         6          1     7          0 |
         +------------------------------------------------+

    Comment


    • #3
      Thanks so much , that is very much appreciated! As always, you have been a huge help.

      And yes, I had forgotten to post the time variable.

      Comment

      Working...
      X