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

st: RE: RE: RE: is ordering with -bysort- unique?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: RE: is ordering with -bysort- unique?
Date   Tue, 10 Jun 2003 23:38:41 +0100

Jose Luis Negrin Muņoz

> > I have used the following small program to get rid of duplicates
> >
> > 	sort ref;
> > 	by ref: gen dup=_n;
> > 	gsort ref -mesini dup;
> > 	drop if dup>1;
> >
> > where ref is a reference code and mesini is a variable
> > related to time
> > (1, 2...j); these are the variables I am sorting my data
> > by. So I will
> > keep only the oldest data with each particular reference code
>
> Two comments:
>
> 1. This code appears to keep the most recent
> observation within each group, not the oldest.
> Another way to do it is
>
> bysort ref (mesini) : keep if _n == _N
>

Jose's code needs more comment. He had

 	sort ref;
 	by ref: gen dup=_n;
 	gsort ref -mesini dup;
 	drop if dup>1;

The -gsort- makes no difference to what is dropped, so
in terms of what is kept (although not in what order)
Jose's code is just

 	sort ref;
 	by ref: gen dup=_n;
 	drop if dup>1;

which is equivalent to

	bysort ref: drop if _n > 1

or

	bysort ref : keep if _n == 1

The problem here is what is first (_n == 1) within
each group of -ref- is not guaranteed to be
the same observation, let alone the time desired.
This is what was spelled out in the examples in my
original reply to Radu.

Nick
n.j.cox@durham.ac.uk

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