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

Re: st: How to quickly merge by the range of a third variable?


From   Rafal Raciborski <rafal.raciborski@emory.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: How to quickly merge by the range of a third variable?
Date   Mon, 10 Oct 2005 13:30:22 -0400

Sean,

Why not something like this?:


. use person, clear

. list

+--------------------------+
| personid bday bplace |
|--------------------------|
1. | 1 1950 city1 |
2. | 2 1951 city2 |
3. | 3 1987 city2 |
4. | 4 1950 city3 |
5. | 5 1975 city1 |
+--------------------------+

. use c1951, clear

. list

+-----------------------+
| city year pop |
|-----------------------|
1. | city1 1951 10250 |
2. | city2 1951 301872 |
3. | city3 1951 65790 |
+-----------------------+

. use c1950, clear

. list

+-----------------------+
| city year pop |
|-----------------------|
1. | city1 1950 10000 |
2. | city2 1950 300000 |
3. | city3 1950 65000 |
+-----------------------+

. append using c1951

. sort city year

. list

+-----------------------+
| city year pop |
|-----------------------|
1. | city1 1950 10000 |
2. | city1 1951 10250 |
3. | city2 1950 300000 |
4. | city2 1951 301872 |
5. | city3 1950 65000 |
|-----------------------|
6. | city3 1951 65790 |
+-----------------------+

. save city_big, replace
file city_big.dta saved

.
. use person, clear

. rename bday year

. rename bplace city

. sort city year

. save person1, replace
file person1.dta saved

.
. merge city year using city_big

. drop if _merge==2
(3 observations deleted)

. drop _merge

. list

+----------------------------------+
| personid year city pop |
|----------------------------------|
1. | 1 1950 city1 10000 |
2. | 5 1975 city1 . |
3. | 2 1951 city2 301872 |
4. | 3 1987 city2 . |
5. | 4 1950 city3 65000 |
+----------------------------------+

.
end of do-file


If your city datasets do not have a year variable in them, you can easliy add it
using -forvalues-. Assuming your city datasets are named c1950, c1951, ... ,
c2000, you can use:

forvalues i = 1950/2000 {
use c`i', clear
gen year = `i'
save, replace
}

and then append and merge.

Hope this helps.

rafal


================
Rafal Raciborski
Graduate student
Department of Political Science
Emory University
301 Tarbutton Hall
1555 Dickey Drive
Atlanta, GA 30322
404-378-9826 (home)
rafal.raciborski@emory.edu
http://www.roofoos.net/


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