Statalist The Stata Listserver

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

st: retry: copy value withing group

Subject   st: retry: copy value withing group
Date   Mon, 23 Oct 2006 09:46:10 -0400

My appologies. The first several lines of my previous e-mail were mysteriously deleted. Hopefully this one is complete.

*Intro*: Here's a simple Stata task I solved years ago and do 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

*Comment*: This works because I choose an -egen- command that ignores missing values. What displeases me about this is that when I review my code later, it is hard to tell what I'm really doing since my motivation is not to compute a mean or max, per se, but just to execute a sort of copy operation within group. What would be nice is if there were a command named copy() (or something clear) that (1) verified the properties I require (see "Fact" above) and then (2) did the operation I want (see "Operation" above). Even a more transparent way of doing (2) (and not (1)) would be an improvement. I'm not skilled enough to easily write such a command. Perhaps there is such a command or some other way to do what I want that is more transparent than my solution. Any suggestions?


* For searches and help try:

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