[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: copy value within group
> frequently, yet I have never been happy about how I do it. I've always
> wondered if there was another way.
> *Fact*: In my data there exists a variable, x, with the following
> property (known by construction or verified with an -assert-). Within
> each group of observations identified by a unique id (call the variable
> id), x is missing for all but one observation.
> *Operation*: I want to assign the single nonmissing value of x to all
> missing values of x within the group. That is, when I'm done, x is
> nonmissing for all observations and constant within id. This constant
> value of x (within id) remains unchanged for the original nonmissing
> values of x.
> *My Solution*: Usually I accomplish this with something like:
> bysort id: egen t = mean(x) //other -egen- commands work, like -max()-//
> replace x = t drop t
Tasks like this should be generally tackled by using -by- and explicit
subscripts. This should work:
. bysort id (x): replace x = x
(More on how to use explicit subscribts can be found under -help
subscripting-, and in chapter 5 of Kohler/Kreuter, Data Analyis Using Stata.)
+49 (030) 25491-361
* For searches and help try: