Announcement

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

  • Panel data- creating dummies

    Hello everyone!

    I am working with panel data. I need to create a dummy variable from the "balances" variable. The "balances" variable shows values and missings for 36 months for each person. The dummy must be a variable that takes the value of one from the first observation greater than zero until the panel ends. That is, if there are panels full of missings, everything should be zero. Additionally, all observations before the first value greater than zero should be zero. But if there are missings after the first observation greater than zero, it should take the value of one. This for each individual analyzed. Thank you very much for your help!

  • #2
    This seems clear, but a real(istic) data example would also have helped: see especially https://www.statalist.org/forums/help#stata

    Otherwise here is some technique. Note that there has been a relevant FAQ since 2007: https://www.stata.com/support/faqs/d...t-occurrences/

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id balances year)
    1  0 2018
    1  0 2019
    1  0 2020
    1  1 2021
    1  1 2022
    2  . 2018
    2 42 2019
    2  1 2020
    2  1 2021
    2  . 2022
    3  . 2018
    3  . 2019
    3  . 2020
    3  . 2021
    3  . 2022
    4  7 2018
    4  7 2019
    4  7 2020
    4  7 2021
    4  7 2022
    end
    
    bysort id (year) : gen wanted = sum(balances > 0 & balances < .) >= 1 
    
    list, sepby(id)
    
         +-------------------------------+
         | id   balances   year   wanted |
         |-------------------------------|
      1. |  1          0   2018        0 |
      2. |  1          0   2019        0 |
      3. |  1          0   2020        0 |
      4. |  1          1   2021        1 |
      5. |  1          1   2022        1 |
         |-------------------------------|
      6. |  2          .   2018        0 |
      7. |  2         42   2019        1 |
      8. |  2          1   2020        1 |
      9. |  2          1   2021        1 |
     10. |  2          .   2022        1 |
         |-------------------------------|
     11. |  3          .   2018        0 |
     12. |  3          .   2019        0 |
     13. |  3          .   2020        0 |
     14. |  3          .   2021        0 |
     15. |  3          .   2022        0 |
         |-------------------------------|
     16. |  4          7   2018        1 |
     17. |  4          7   2019        1 |
     18. |  4          7   2020        1 |
     19. |  4          7   2021        1 |
     20. |  4          7   2022        1 |
         +-------------------------------+

    Comment

    Working...
    X