Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: copy value within group


From   Ulrich Kohler <[email protected]>
To   [email protected]
Subject   Re: st: copy value within group
Date   Mon, 23 Oct 2006 16:13:33 +0200

[email protected] wrote:
> 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[1]

(More on how to use explicit subscribts can be found under -help 
subscripting-, and in chapter 5 of Kohler/Kreuter, Data Analyis Using Stata.) 

Many regards

Uli

-- 
[email protected]
+49 (030) 25491-361
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index