Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: missing data
From 
 
Nick Cox <[email protected]> 
To 
 
[email protected] 
Subject 
 
Re: st: missing data 
Date 
 
Thu, 6 Oct 2011 00:57:36 +0100 
Various ways. I assume that you want this done if and only if the
non-missing values are consistent within -subjectid-. This code
assumes, contrary to some of your indications, that all variables
concerned are numeric.
foreach v in exposurea exposureb {
      clonevar copy = `v'
      bysort subjectid (`v') : replace copy = copy[_n-1] if missing(copy)
      by subjectid : replace `v' = copy if copy[1] == copy[_N]
      drop copy
}
For a string variable, this would be
foreach v in somestrvar {
      gen notmissing = !missing(`v')
      clonevar copy = `v'
      bysort subjectid (notmissing `v') : replace copy = copy[_n-1] if
missing(copy)
      by subjectid : replace `v' = copy if copy[1] == copy[_N]
      drop copy notmissing
}
and that code should also work for numeric variables.
See also
FAQ     . . . . . . . . . . . . . . . . . . . . . . . Replacing missing values
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        2/03    How can I replace missing values with previous or
                following nonmissing values?
                http://www.stata.com/support/faqs/data/missing.html
Nick
On Thu, Oct 6, 2011 at 12:35 AM, Michael Eisenberg
<[email protected]> wrote:
> I have a list of data for patients and some subjects have more than
> one row.  Unfortunately, the data entry was not perfect so that some
> fields that should be the same for each individual (like an exposure)
> is not.
>
> subjectid     exposurea      exposureb
> 1                    43                     bz
> 1                     .                        .
> 1                     .                        .
>
> 3                     .                        .
> 3                     .                        .
> 3                    888                   qx
>
>
> How can I get all fields to populate with the non missing data so that
> it looks like
>
> subjectid     exposurea      exposureb
> 1                    43                     bz
> 1                     43                    bz
> 1                    43                     bz
>
> 3                    888                    qx
> 3                    888                    qx
> 3                    888                   qx
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/