Announcement

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

  • Extract entries across rows?

    Dear Statalist,

    Imagin 3 players transmitting messages, we say a player transmits a message correctly if she copied the same message with her upstream player.
    Now I have the data of all the copied messages and the sequence of players but I don't know how to create the new variable recording whether she copied the upstream message correctly.
    i.e. whether the message she copied is the same as her upstream message.


    Code:
        input id entry1 entry2 entry3
        1  123 456 789
        2  789 123 456
        3  456 789 123
        end
    I want to generate a new variable say whether entry2 is copied correctly. If player2's entry 2 = player1's entry1, we say player2 copied correctly.
    Something like the following (which is not working "invalid syntax"):

    Code:
    bysort id: gen correct2=1 if id==2 & entry2[2]==entry1[1]
    Any help would be much appreciated!

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id entry1 entry2 entry3)
    1 123 456 789
    2 789 123 456
    3 456 789 123
    end
    
    sort id
    gen wanted= entry2== entry1[_n-1] & entry3== entry2[_n-1]
    Res.:

    Code:
    . gen wanted= entry2== entry1[_n-1] & entry3== entry2[_n-1]
    
    . l
    
         +----------------------------------------+
         | id   entry1   entry2   entry3   wanted |
         |----------------------------------------|
      1. |  1      123      456      789        0 |
      2. |  2      789      123      456        1 |
      3. |  3      456      789      123        1 |
         +----------------------------------------+

    Comment


    • #3
      Many many thanks! Andrew!

      Comment

      Working...
      X