Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: How can I combine datasets


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: How can I combine datasets
Date   Fri, 16 Sep 2011 09:38:41 +0200

On Thu, Sep 15, 2011 at 10:04 PM, Kevin Beckwith wrote:
> I am currently trying to combine two datasets with different variables over the same five years.  Both sets are on the county level, but county definitions change over time.  I've tried merge and append, but neither is giving me the results I desire.   Is there a simple way of combining these sets such that it only keeps counties that are present in all years and in both sets?

If I understand your problem correctly than in the example below we
only want counties 1 and 2. We want to exclude county 3 because it is
incomplete and county 4 because it only appears in dataset b.

*----------- begin example ----------
// create some example datasets
tempfile a b
drop _all
input ///
county year x
1      2000 1
1      2001 2
1      2003 4
2      2001 5
2      2002 2
2      2003 4
3      2002 2
3      2003 1
end
save `a'

drop _all
input ///
county year y
1      2000 1
1      2001 2
1      2003 4
2      2001 5
2      2002 2
2      2003 4
3      2002 2
3      2003 1
4      2001 5
4      2002 2
4      2003 4
end
save `b'

// merge the files
use `a'
merge 1:1 county year using `b'

// keep only those observations that
// appeared in both datasets
keep if _merge == 3

// keep only those counties that
// are observed in all (3) years
bys county : gen Nyears = _N
keep if Nyears == 3

// clean up
drop _merge Nyears

// see the result
list
*------------ end example -----------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany


http://www.maartenbuis.nl
--------------------------

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index