Announcement

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

  • Create dummy variable in long dataset

    I have a long dataset, and I want to create a dummy variable to indicate whether a participant reached the close-out phase of the study. So if any status="Close-out", dummy=1. If status never equals "Close-out", dummy=0. How can I generate this dummy variable?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int study_id str9 status
    1127 "Follow-up"
    1127 "Close-out"
    1250 "Follow-up"
    1250 "Close-out"
    1619 "Follow-up"
    1619 "Close-out"
    1639 "Close-out"
    1639 "Follow-up"
    1747 "Follow-up"
    1747 "Close-out"
    1751 "PT2"      
    1769 "PT2"      
    1777 "PT2"      
    1802 "PT2"      
    1817 "PT2"      
    1872 "Follow-up"
    1872 "Close-out"
    1943 "Close-out"
    1943 "Follow-up"
    1944 "PT2"      
    1945 "Follow-up"
    1945 "Close-out"
    1955 "Close-out"
    1955 "Follow-up"
    1956 "Close-out"
    1956 "Follow-up"
    1996 "PT2"      
    1996 "Follow-up"
    1996 "Close-out"
    2002 "Close-out"
    2002 "Follow-up"
    2023 "Close-out"
    2023 "Follow-up"
    2030 "Close-out"
    2030 "PT2"      
    2030 "Follow-up"
    2031 "PT2"      
    2031 "Follow-up"
    2031 "Close-out"
    2042 "Close-out"
    2042 "Follow-up"
    2055 "Follow-up"
    2055 "Close-out"
    2055 "PT2"      
    2061 "Close-out"
    2061 "Follow-up"
    2079 "Close-out"
    2079 "Follow-up"
    2128 "Close-out"
    2128 "Follow-up"
    2140 "Close-out"
    2140 "Follow-up"
    2148 "Close-out"
    2161 "Close-out"
    2161 "Follow-up"
    2183 "Close-out"
    2183 "Follow-up"
    2183 "Close-out"
    2183 "Follow-up"
    2199 "Follow-up"
    2199 "Close-out"
    2204 "Close-out"
    2204 "Follow-up"
    2211 "Close-out"
    2211 "Follow-up"
    2211 "PT2"      
    2223 "Follow-up"
    2223 "Close-out"
    2234 "PT2"      
    2234 "Close-out"
    2238 "Close-out"
    2238 "Follow-up"
    2259 "Close-out"
    2259 "Follow-up"
    2268 "Close-out"
    2268 "Follow-up"
    2280 "Close-out"
    2280 "Follow-up"
    2350 "Baseline" 
    2355 "Follow-up"
    2355 "PT2"      
    2355 "Close-out"
    2362 "Close-out"
    2363 "Close-out"
    2363 "PT2"      
    2363 "Follow-up"
    2368 "Close-out"
    2368 "Follow-up"
    2369 "Follow-up"
    2369 "PT2"      
    2369 "Close-out"
    2370 "Follow-up"
    2370 "Close-out"
    2370 "PT2"      
    2372 "Close-out"
    2388 "PT1"      
    2388 "Baseline" 
    2388 "Close-out"
    2388 "Follow-up"
    2388 "PT2"      
    end

  • #2
    See https://www.stata.com/support/faqs/d...ble-recording/.

    Code:
    bys study_id: egen wanted= max(status=="Close-out")

    Res.:

    Code:
    . list, sepby(study_id)
    
         +-------------------------------+
         | study_id      status   wanted |
         |-------------------------------|
      1. |     1127   Follow-up        1 |
      2. |     1127   Close-out        1 |
         |-------------------------------|
      3. |     1250   Follow-up        1 |
      4. |     1250   Close-out        1 |
         |-------------------------------|
      5. |     1619   Follow-up        1 |
      6. |     1619   Close-out        1 |
         |-------------------------------|
      7. |     1639   Close-out        1 |
      8. |     1639   Follow-up        1 |
         |-------------------------------|
      9. |     1747   Follow-up        1 |
     10. |     1747   Close-out        1 |
         |-------------------------------|
     11. |     1751         PT2        0 |
         |-------------------------------|
     12. |     1769         PT2        0 |
         |-------------------------------|
     13. |     1777         PT2        0 |
         |-------------------------------|
     14. |     1802         PT2        0 |
         |-------------------------------|
     15. |     1817         PT2        0 |
         |-------------------------------|
     16. |     1872   Follow-up        1 |
     17. |     1872   Close-out        1 |
         |-------------------------------|
     18. |     1943   Close-out        1 |
     19. |     1943   Follow-up        1 |
         |-------------------------------|
     20. |     1944         PT2        0 |
         |-------------------------------|
     21. |     1945   Follow-up        1 |
     22. |     1945   Close-out        1 |
         |-------------------------------|
     23. |     1955   Close-out        1 |
     24. |     1955   Follow-up        1 |
         |-------------------------------|
     25. |     1956   Close-out        1 |
     26. |     1956   Follow-up        1 |
         |-------------------------------|
     27. |     1996         PT2        1 |
     28. |     1996   Follow-up        1 |
     29. |     1996   Close-out        1 |
         |-------------------------------|
     30. |     2002   Close-out        1 |
     31. |     2002   Follow-up        1 |
         |-------------------------------|
     32. |     2023   Close-out        1 |
     33. |     2023   Follow-up        1 |
         |-------------------------------|
     34. |     2030   Close-out        1 |
     35. |     2030         PT2        1 |
     36. |     2030   Follow-up        1 |
         |-------------------------------|
     37. |     2031         PT2        1 |
     38. |     2031   Follow-up        1 |
     39. |     2031   Close-out        1 |
         |-------------------------------|
     40. |     2042   Close-out        1 |
     41. |     2042   Follow-up        1 |
         |-------------------------------|
     42. |     2055   Follow-up        1 |
     43. |     2055   Close-out        1 |
     44. |     2055         PT2        1 |
         |-------------------------------|
     45. |     2061   Close-out        1 |
     46. |     2061   Follow-up        1 |
         |-------------------------------|
     47. |     2079   Close-out        1 |
     48. |     2079   Follow-up        1 |
         |-------------------------------|
     49. |     2128   Close-out        1 |
     50. |     2128   Follow-up        1 |
         |-------------------------------|
     51. |     2140   Close-out        1 |
     52. |     2140   Follow-up        1 |
         |-------------------------------|
     53. |     2148   Close-out        1 |
         |-------------------------------|
     54. |     2161   Close-out        1 |
     55. |     2161   Follow-up        1 |
         |-------------------------------|
     56. |     2183   Close-out        1 |
     57. |     2183   Follow-up        1 |
     58. |     2183   Close-out        1 |
     59. |     2183   Follow-up        1 |
         |-------------------------------|
     60. |     2199   Follow-up        1 |
     61. |     2199   Close-out        1 |
         |-------------------------------|
     62. |     2204   Close-out        1 |
     63. |     2204   Follow-up        1 |
         |-------------------------------|
     64. |     2211   Close-out        1 |
     65. |     2211   Follow-up        1 |
     66. |     2211         PT2        1 |
         |-------------------------------|
     67. |     2223   Follow-up        1 |
     68. |     2223   Close-out        1 |
         |-------------------------------|
     69. |     2234         PT2        1 |
     70. |     2234   Close-out        1 |
         |-------------------------------|
     71. |     2238   Close-out        1 |
     72. |     2238   Follow-up        1 |
         |-------------------------------|
     73. |     2259   Close-out        1 |
     74. |     2259   Follow-up        1 |
         |-------------------------------|
     75. |     2268   Close-out        1 |
     76. |     2268   Follow-up        1 |
         |-------------------------------|
     77. |     2280   Close-out        1 |
     78. |     2280   Follow-up        1 |
         |-------------------------------|
     79. |     2350    Baseline        0 |
         |-------------------------------|
     80. |     2355   Follow-up        1 |
     81. |     2355         PT2        1 |
     82. |     2355   Close-out        1 |
         |-------------------------------|
     83. |     2362   Close-out        1 |
         |-------------------------------|
     84. |     2363   Close-out        1 |
     85. |     2363         PT2        1 |
     86. |     2363   Follow-up        1 |
         |-------------------------------|
     87. |     2368   Close-out        1 |
     88. |     2368   Follow-up        1 |
         |-------------------------------|
     89. |     2369   Follow-up        1 |
     90. |     2369         PT2        1 |
     91. |     2369   Close-out        1 |
         |-------------------------------|
     92. |     2370   Follow-up        1 |
     93. |     2370   Close-out        1 |
     94. |     2370         PT2        1 |
         |-------------------------------|
     95. |     2372   Close-out        1 |
         |-------------------------------|
     96. |     2388         PT1        1 |
     97. |     2388    Baseline        1 |
     98. |     2388   Close-out        1 |
     99. |     2388   Follow-up        1 |
    100. |     2388         PT2        1 |
         +-------------------------------+
    
    .

    Comment

    Working...
    X