Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: merge


From   "Maarten Buis" <[email protected]>
To   <[email protected]>
Subject   st: RE: merge
Date   Fri, 28 Jul 2006 11:10:45 +0200

--- Daniel asked:
> I'm using Stata 9.1. In DBQ.dta, variables Year and Quarter uniquely identify
> all observations (i.e. quarterly data). However, in DBM.dta this is not the
> case since this database contains monthly data.
>
> Typing
> . merge Year Quarter using DBQ.dta, uniqusing sort
> 
> Yields the following error message:
> variables Year Quarter do not uniquely identify observations in the master data
> r(459);
>  
> Although Stata's observation is correct, I wonder why this error occurs.
> Actually, Stata's error message is what I wanted to express in the -merge-
> command... ("1:n merge")

Daniel:
The problem with you command is the sort option. The helpfile of -merge- states:

"sort specifies that the master and using datasets are to be sorted by the match
variables, before the datasets are merged, if they are not already sorted by them.
Match variables are required with sort.  sort implies unique."

It says about unique that: "unique is thus equivalent to specifying uniqmaster 
and uniqusing."

and it says about uniqmaster: "uniqmaster specifies that the match variables 
uniquely identify the observations in memory, the master data, but not 
necessarily the ones in the using data."

That last condition is violated in your data.

The solution to that problem is to sort both datasets before using -merge- and 
don't use the sort option. See the example below:

*--------------begin example---------------
sysuse auto, clear
tempfile collap
collapse (mean) price, by(rep78)
rename price mprice
sort rep78
save `collap', replace
sysuse auto, clear
sort rep78
merge rep78 using `collap', uniqusing
tab _merge
list rep78 mprice in 1/10
*------------end example-------------

HTH,
Maarten
        
-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology 
Vrije Universiteit Amsterdam 
Boelelaan 1081 
1081 HV Amsterdam 
The Netherlands

visiting adress:
Buitenveldertselaan 3 (Metropolitan), room Z434 

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------



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