Statalist


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

st: RE: Variable generation


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Variable generation
Date   Mon, 11 Feb 2008 17:44:36 -0000

bysort hid (var1) : gen var2 = var1[1] 

For more detail, see 

FAQ    Replacing missing values
How can I replace missing values with previous or following nonmissing
values?
http://www.stata.com/support/faqs/data/missing.html

Your problem is only a twist away from that discussed in the FAQ. 

However, in general you need to check that each -hid- is characterised
by at most one non-missing value on -var1-. See also 

FAQ Number of distinct observations
How do I compute the number of distinct observations?
http://www.stata.com/support/faqs/data/distinct.html

Nick 
n.j.cox@durham.ac.uk 

Mentzakis, Emmanouil

I have a variable that identifies the household (hid), a variable that
identifies the number that each individual has within the household
(pid) and finally a variable that has some values of interest (present
only for some individuals).

I would like to create a new variable (var2) that has the value of var1
for all the pid in the same hid (as in the example below).

hid	pid	var1	var2        
1	1	10	10           
1	2		10               
2	1		6              
2	2	6	6            
2	3	6	6             
2	4		6              
3	4	8	8                 
3	5		8	               

One way to do it is:  
by hid: egen var2=mean(var1)   

However, I was wondering if there was any command that rather than
taking averages, could copy that value to everybody with the same hid.

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index