Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: data manipulation question


From   Scott Merryman <scott.merryman@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: data manipulation question
Date   Wed, 11 Aug 2010 11:45:53 -0500

Something like this?

clear*
input id  race  quarter     move      nn1     nn2       nn3
1      1          1          0          2          3         4
1      1          2          0          2          3         4
1      1          3          1          2          3         4
2      0          1          0          1          4         3
2      0          2          1          1          4         3
3      0          1          0           5         1         4
3      0          2          1           5         1         4
4      0          1          0           3         2         5
4      0          2          0           3         2         5
4      0          3          1           3         2         5
5      1          1          0           3         2         1
5      1          2          1           3         2         1
end
gen race2 = race
replace race2 = 2 if race2 ==0
l,sepby(id)
tempfile foo

//nn1
sort nn1
save `foo',replace
keep if move >0
keep id race2 quart
rename id nn1
rename race2 move_nn1
sort nn1
merge 1:m nn1 quarter using `foo'
keep if _m >1
drop _m
replace move_nn1 = 0 if move_nn1 ==.
sort id qua

//nn2
sort nn2
save `foo',replace
keep if move >0
keep id race2 quart
rename id nn2
rename race2 move_nn2
sort nn2
merge 1:m nn2 quarter using `foo'
keep if _m >1
drop _m
replace move_nn2 = 0 if move_nn2 ==.


//nn3
sort nn3
save `foo',replace
keep if move >0
keep id race2 quart
rename id nn3
rename race2 move_nn3
sort nn3
merge 1:m nn3 quarter using `foo'
keep if _m >1
drop _m
replace move_nn3 = 0 if move_nn3 ==.
sort id qua
order id race race2 quar move nn1 nn2 nn3 move_nn1 move_nn2 move_nn3
l, sepby(id)


Scott


On Wed, Aug 11, 2010 at 10:01 AM, DC <dcase79@gmail.com> wrote:
> Hi Scott,
>
> Sorry if it is confusing.
>
> move is binary for whether unit i (id) moved or not in that particular quarter.
> Hence, for id=1, he doesn't move in quarter 2
>
> What happens here is that id =1 does not move in quarter 2, so
> therefore move = 0. However, in quarter 2
>  the first nearest neighbor (id =2)  and second nearest neighbor
> (id=3) moved and they were both of race 2. Hence,
> I assigned the racial indicator for each nearest neighbor who moved to
> move_nn1 and move_nn2.
>
> In the next quarter, quarter 3, id =1 moved. In same quarter, nearest
> neighbor 3 moved as well and he was also of race 2.
> hence, I assigned 2 to move_nn3 to denote that nearest neighbor 3
> moved and they were of race 2.
>
> Perhaps,a better way to do it is to just assign a binary indicator for
> whether nearest neighbor 1 and nearest neighbor 2
> moved and put the race in a separate variable.
>
> Thanks for any assistance.
>
> Regards,
> Marcus

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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   |   Site index