Statalist The Stata Listserver

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

st: RE: merge

From   "Maarten Buis" <>
To   <>
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")

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

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

*   For searches and help try:

© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index