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: RE: RE: replace blank string values


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: RE: replace blank string values
Date   Thu, 1 Jul 2010 11:02:19 +0100

Fine, but if the data sometimes have missing names then there may be
other problems too, perhaps other -name-s for the same -id-. A check,
once you have done this, that -name- is now constant within -id- is 

bysort id (name) : gen nameprob = name[1] != name[_N] 
list id name if nameprob 

If no such observations are shown, then -nameprob- may be dropped. 

See also, following Martin's wildcard reference down to details, 

FAQ     . . . . . .  Listing observations in a group that differ on a
variable
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
J. Cox
        11/01   How do I list observations in a group that differ
                on a variable?
                http://www.stata.com/support/faqs/data/diff.html

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 
n.j.cox@durham.ac.uk 

Martin Weiss

My code telescoped your two lines into

***********
bysort id (name): replace name=name[_N]
***********

But that is not strictly necessary...

The FAQ at http://www.stata.com/support/faqs/data/#manip are definitely
worth perusing for you. Full of neat tricks...

richard boylan

Sorting by name is the trick, thank you.

sort id name
by id: replace name = name[_N]

On Wed, Jun 30, 2010 at 1:29 PM, Martin Weiss <martin.weiss1@gmx.de>
wrote:

> Throw in a -sort- after -expand- to make this work:
>
>
> ***********
> ...
> expand 3
> sort id
> inp str10 name
> ...
> ***********

Martin Weiss

> -sort-ing allocates the name to the end, so you can pick it up
there...
>
> ***********
> clear*
> set obs 5
> gen byte id=_n
> expand 3
>
> inp str10 name
> ""
> Nick
> ""
> Kit
> ""
> ""
> ""
> Ben
> ""
> Maarten
> ""
> ""
> ""
> ""
> Martin
>
> l, sepby(id)
> bys id (name): replace name=name[_N]
> l, sepby(id)
> ***********

richard boylan

> I have a dataset which is a panel: id, year where id is the numeric
> value the individual.
>
> I also have the name of the individual, however, for some
> observations, the name is missing.
>
> How do I go about replacing the missing values for name?
>
> One way that works is
> sort id year
> by id: replace name = name[_n + 1] if name == "" & name[_n + 1] ~= ""
> by id: replace name = name[_n + 1] if name == "" & name[_n + 1] ~= ""
> ...
> by id: replace name = name[_n + 1] if name == "" & name[_n + 1] ~= ""
>
> However, is there a more efficient way of doing this, say using egen?

*
*   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