Announcement

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

  • How To Fill Missing Values

    Hi all,

    I'm currently working a dataset in which some of the values of the variable are missing. I'm unable to share the data, therefore, I'm giving an example below for a context. For example, as you can see that IDs are recorded repeatedly with visits but values are missing for IDs. How can I fill those missing values according to each ID once a value of Visit is created. In other words, (example below) I want to fill all missing values of ID (1)= 10. And for all ID (4) = 20 , for all ID (6) = 20 and so on. Please note that there are only two values for Visit variable. I would be very grateful if anybody could help me with a particular code on this problem. Please let me know if you need further clarification. Thank you so much.


    Variables:

    ID: Visit:
    1 10
    1 10
    1
    2 10
    2
    2
    3
    4 20
    4
    4
    4
    5 10
    5
    6
    6
    6 20
    6
    6



  • #2
    Perhaps this will start you in a useful direction.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id visit)
    1 10
    1 10
    1  .
    2 10
    2  .
    2  .
    3  .
    4 20
    4  .
    4  .
    4  .
    5 10
    5  .
    6  .
    6  .
    6 20
    6  .
    6  .
    end
    egen v = max(visit), by(id)
    replace visit = v if visit==.
    list, sepby(id)
    Code:
    . list, sepby(id)
    
         +-----------------+
         | id   visit    v |
         |-----------------|
      1. |  1      10   10 |
      2. |  1      10   10 |
      3. |  1      10   10 |
         |-----------------|
      4. |  2      10   10 |
      5. |  2      10   10 |
      6. |  2      10   10 |
         |-----------------|
      7. |  3       .    . |
         |-----------------|
      8. |  4      20   20 |
      9. |  4      20   20 |
     10. |  4      20   20 |
     11. |  4      20   20 |
         |-----------------|
     12. |  5      10   10 |
     13. |  5      10   10 |
         |-----------------|
     14. |  6      20   20 |
     15. |  6      20   20 |
     16. |  6      20   20 |
     17. |  6      20   20 |
     18. |  6      20   20 |
         +-----------------+

    Comment

    Working...
    X