Announcement

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

  • How do I loop through parts of the observations and impute missing values

    Dear Statalist,

    My data looks like this
    id hypertension visit
    1 . 1
    1 0 2
    2 . 1
    2 . 2
    2 1 3
    3 . 1
    4 . 1
    5 0 1
    5 1 2
    5 . 3
    (hypertension 1=yes;0=no; . =missing value)(Every ID had 1-3 visits)
    I'm trying to impute all missing values according to the following rule.
    "If an ID has hypertension, this ID must have hypertension in the future."
    ex: ID1 didn't have hypertension on his second visit, so he didn't have hypertension on his first visit either.
    ex: ID5 had hypertension on his second visit, so he must had hypertension on his third visit as well.

    There are hundreds of ID in my data, so I'm trying to use loop to solve the problem.
    I found something like increment might be related.
    Can anyone give me more information?
    Thanks!
    Last edited by Ian Wang; 14 Apr 2019, 04:41.

  • #2
    this would have been easier if you had used -dataex-; please see the FAQ; the following code does what I think you want (though your rules do not cover all possibilities):
    Code:
    bys id (visit): replace hy=hy[_n-1] if hy[_n-1]==1 & hy==. & id==id[_n-1]
    gsort id -visit
    by id: replace hy=hy[_n-1] if hy[_n-1]==0 & hy==. & id==id[_n-1]
    sort id visit
    this gives the following result:
    Code:
    . li, clean noo
    
        id   hy   visit  
         1    0       1  
         1    0       2  
         2    .       1  
         2    .       2  
         2    1       3  
         4    .       1  
         5    0       1  
         5    1       2  
         5    1       3
    as you can see, no loop is needed

    Comment


    • #3
      I tried and it worked!
      Thank you.

      Comment


      • #4
        See e.g. https://www.stata.com/support/faqs/d...issing-values/

        Comment

        Working...
        X