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: copying a string variable to all rows within a group
From
<[email protected]>
To
<[email protected]>
Subject
Re: st: copying a string variable to all rows within a group
Date
Wed, 29 Feb 2012 11:31:03 +0000
Thanks Nick. I was using search terms (phrases including egen, copy, string, row, etc) that didn't bring up that particular FAQ.
These two do as I expected, however they don't do exactly the same thing (discrepancy of 2-3 in total number of changes made):
1. bysort groupvar (oldvar) : replace oldvar = oldvar[_N]
2. bysort groupvar : replace oldvar = oldvar[_n-1] if missing(oldvar) & _n > 1
This one on the other hand appears to do something completely different - including returning many of the original (single) observations to missing:
3. bysort groupvar : replace oldvar = oldvar[1]
I've used the first option as this results in the largest number of changes (3 more than second option), and intuitively seems more accurate - maybe not the most robust of reasons but no other more sensible ones come to mind I'm afraid.
thanks,
Seyi
>>> Nick Cox <[email protected]> 2/29/2012 9:01 AM >>>
For "row" read "observation". We know what you mean, but that is the
correct Stata terminology.
bysort groupvar (oldvar) : replace oldvar = oldvar[_N]
Alternatively,
bysort groupvar : replace oldvar = oldvar[1]
will probably work too. Alternatively,
bysort groupvar : replace oldvar = oldvar[_n-1] if missing(oldvar) & _n > 1
will probably work too. This last is an FAQ
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
On Wed, Feb 29, 2012 at 8:28 AM, <[email protected]> wrote:
,
> If I have a numeric variable "oldvar" which appears on only the first row of a set of rows defined by a group variable "groupvar" (rest of oldvar rows are blank), it is easy enough to copy oldvar down all rows within groupvar:
>
> bysort groupvar: egen newvar=max(oldvar) // can equally use =min(oldvar) as there is only 1 value of oldvar.
>
> How might I do the same to copy a string oldvar to all rows - again it only appears once within the group, but not necessarily on the first row.
>
*
* 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/
*
* 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/