Announcement

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

  • Generate a variable based on id number and its chosen indicator

    Dear Stata users,

    I encounter this question when using one-to-one matching. The variable id is number of observations, the variable chosen stores the observation number of the matched observation. I want to generate an indicator variable (i.e. matched) that equals 1 if id number can be found in corresponding variable chosen, and equals 0 if not. For example, matched in line three will take value 1 because you can find number 3 in variable chosen, line six also takes value 1 because you can find number 6 in variable chosen. I can't find a solution, hope someone could help me. Thank you!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id chosen matched)
     1  . 1
     2  . .
     3  . 1
     4  . .
     5  . .
     6  . 1
     7  . 1
     8  . .
     9  . 1
    10  . .
    11  . 1
    12  . .
    13  . .
    14  . 1
    15  . .
    16  . .
    17  . 1
    18  . .
    19  1 .
    20  3 .
    21  6 1
    22  7 1
    23  9 .
    24 11 1
    25 14 .
    26 17 1
    27 21 .
    28 22 .
    29 24 .
    30 26 .
    end
    label var id "observations ID"
    label var chosen "chosen observations (value==id)"
    label var matched "indicator that observation was choosed"

  • #2
    If you search the forum, there are many examples of this kind of problem. Here is a solution first suggested by Romalpa Akzo

    Code:
    expand 2, gen(set)
    replace id=chosen if set
    bysort id: egen wanted = max(id==id[_n+1]) if !missing(id)
    drop if set
    Result:

    Code:
    . list id chosen matched wanted
    
         +--------------------------------+
         | id   chosen   matched   wanted |
         |--------------------------------|
      1. |  1        .         1        1 |
      2. |  2        .         .        0 |
      3. |  3        .         1        1 |
      4. |  4        .         .        0 |
      5. |  5        .         .        0 |
         |--------------------------------|
      6. |  6        .         1        1 |
      7. |  7        .         1        1 |
      8. |  8        .         .        0 |
      9. |  9        .         1        1 |
     10. | 10        .         .        0 |
         |--------------------------------|
     11. | 11        .         1        1 |
     12. | 12        .         .        0 |
     13. | 13        .         .        0 |
     14. | 14        .         1        1 |
     15. | 15        .         .        0 |
         |--------------------------------|
     16. | 16        .         .        0 |
     17. | 17        .         1        1 |
     18. | 18        .         .        0 |
     19. | 19        1         .        0 |
     20. | 20        3         .        0 |
         |--------------------------------|
     21. | 21        6         1        1 |
     22. | 22        7         1        1 |
     23. | 23        9         .        0 |
     24. | 24       11         1        1 |
     25. | 25       14         .        0 |
         |--------------------------------|
     26. | 26       17         1        1 |
     27. | 27       21         .        0 |
     28. | 28       22         .        0 |
     29. | 29       24         .        0 |
     30. | 30       26         .        0 |
         +--------------------------------+

    Comment


    • #3
      Thank you Andrew Musau, a nice solution!

      Comment

      Working...
      X