Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: missing data


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: missing data
Date   Thu, 6 Oct 2011 01:04:28 +0100

correction

foreach v in somestrvar {
      gen missing = missing(`v')
      clonevar copy = `v'
      bysort subjectid (missing `v') : replace copy = copy[_n-1] if
 missing(copy)
      by subjectid : replace `v' = copy if copy[1] == copy[_N]
     drop copy missing
}

On Thu, Oct 6, 2011 at 12:57 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> 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
> <meisenberg713@gmail.com> 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index