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   Sean <[email protected]>
To   [email protected]
Subject   Re: st: How to quickly merge by the range of a third variable?
Date   Mon, 10 Oct 2005 12:38:26 -0500

Many thanks Rafal; very helpful indeed. I'm in the process:)

On 10/10/05, Rafal Raciborski <[email protected]> wrote:
> 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)
> [email protected]
> 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/
>


--
Best Regards,

Sean

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