Announcement

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

  • generating a new variable that takes on the value of another variable if a third variable matches a value

    Hello, i want to generate a new variable called "td" that takes on the value of the variable target if rank is 1 when dup is 1 and the value of the variable target if rank is 2 when dup is 2. the first table is the data that i have right now and the second table is the data that i want to have with the new variable. please let me know what i can do!
    Company_id target rank dup
    1 . . 1
    1 . . 1
    1 3 1 1
    1 . . 1
    1 5 2 1
    1 . . 1
    1 7 3 1
    1 . . 2
    1 . . 2
    1 3 1 2
    1 . . 2
    1 5 2 2
    1 . . 2
    1 7 3 2
    1 . . 3
    1 . . 3
    1 3 1 3
    1 . . 3
    1 5 2 3
    1 . . 3
    1 7 3 3
    Company_id target rank dup. td.
    1 . . 1 3
    1 . . 1 3
    1 3 1 1 3
    1 . . 1 3
    1 5 2 1 3
    1 . . 1 3
    1 7 3 1 3
    1 . . 2 5
    1 . . 2 5
    1 3 1 2 5
    1 . . 2 5
    1 5 2 2 5
    1 . . 2 5
    1 7 3 2 5
    1 . . 3 7
    1 . . 3 7
    1 3 1 3 7
    1 . . 3 7
    1 5 2 3 7
    1 . . 3 7
    1 7 3 3 7
    Last edited by Thijs de Jong; 10 Jun 2022, 03:33.

  • #2
    Your wording and your example don't seem to match. Your wording to me implies

    Code:
    gen td = target if (rank == 1 & dup == 1) | (rank == 2 & dup == 2)

    Comment


    • #3
      Hey Nick Cox , thanks for your reaction! when i use this code i only get the value 3 when dup is 1 when target is equal to three but i need it to be 3 on every row where dup is 1. I can't seem to find what i need to change to get it done?

      this is the output that i get when i use your code:
      Company_id target rank dup td
      1 . . 1 .
      1 . . 1 .
      1 3 1 1 3
      1 . . 1 .
      1 5 2 1 .
      1 . . 1 .
      1 7 3 1 .
      1 . . 2 .
      1 . . 2 .
      1 3 1 2 .
      1 . . 2 .
      1 5 2 2 5
      1 . . 2 .
      1 7 3 2 .
      1 . . 3 .
      1 . . 3 .
      1 3 1 3 .
      1 . . 3 .
      1 5 2 3 .
      1 . . 3 .
      1 7 3 3 7
      but i need the output to be like the second table in my original post
      Last edited by Thijs de Jong; 10 Jun 2022, 04:18.

      Comment


      • #4
        Guessing wildly

        Code:
        gen td = target if (rank == 1 & dup == 1) | (rank == 2 & dup == 2)
        
        bysort id (td) : replace td = td[1]
        Last edited by Nick Cox; 10 Jun 2022, 05:36.

        Comment

        Working...
        X