Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: merging variables within groups


From   Joseph Coveney <[email protected]>
To   Statalist <[email protected]>
Subject   st: Re: merging variables within groups
Date   Tue, 27 May 2003 22:31:05 +0900

Gayatri Koolwal posted:

> Hello,
> 
> I have a dataset which includes three variables as follows:
> 
> 
> var	1	2	3	
> 
> hhd1	a	n	o
> hhd1	b	o	.
> 
> hhd2	c	p	r
> hhd2	d	q	.
> hhd2	e	r	.
> 
> hhd3	f	s	o
> hhd3	g	o	.
> .
> .
> .
> .
> 
> 
> that is, variable 2 and 3 share common values (like an id variable), with
> some missing values for variable 3.  i would like to create a fourth variable
> which gives the value for variable 1 (originally in the same row as the
> observation variable 2) in the same row as the observation for variable 3: 
> 
>  
> 	
> var	1	2	3	4
> 
> hhd1	a	n	o	b
> hhd1	b	o	.	.
> 
> hhd2	c	p	r	e
> hhd2	d	q	.	.
> hhd2	e	r	.	.
> 
> hhd3	f	s	o	g
> hhd3	g	o	.	.
> 
> 
> but since the common 'id' variable may be the same for different individuals
> across households, i also wanted to correct for that when matching columns 3
> and 2 (so that 'g' does not get matched to household 1 instead of household
> 3).  is there a way to join these two functions together in Stata?
> 
> Thank you very much,
> 
> Gayatri Koolwal
> 
> 
> 

--------------------------------------------------------------------------------

How about something like in the following do-file?

Joseph Coveney

--------------------------------------------------------------------------------

* next few lines are just inputing the dataset
clear
input str4 not_for_use str1 obs_no str1 id1 str1 id2
hhd1	a	n	o
hhd1	b	o	

hhd2	c	p	r
hhd2	d	q	
hhd2	e	r	

hhd3	f	s	o
hhd3	g	o	
end
* the action starts here
sort obs_no
generate byte id3 = id2!=""
replace id3 = sum(id3)
bysort id3: replace id2 = obs_no[_N]
by id3: replace id2 = "" if _n > 1
exit

--------------------------------------------------------------------------------




*
*   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