# Re: st: dropping even observations

 From Jeph Herrin To statalist@hsphsun2.harvard.edu Subject Re: st: dropping even observations Date Fri, 06 Oct 2006 08:27:04 -0400

One related point, not readily apparent to even regular users,
is whether in

drop if [exp]

expression [exp] is evaluated sequentially for each obs, or
all at once as a vector. My first thought in reading the
question was that the user was asking whether

drop if !mod(_n,2)

would reset _n after each dropped observation; that is, after
dropping _n==2, is the next observation going to be _n==3 or
_n==2?

Obviously, it's not a big deal to

gen byte even = !mod(_n,2)
drop if even

but it's still worth knowing whether one needs the extra step.

Jeph

Nick Cox wrote:
The following question and reply arose privately. I have been asked this various times before, so the discussion should be of some wider interest.
Nick
n.j.cox@durham.ac.uk
Question: Do you have an expression or do file I could use or adapt to drop even rows (_n = 2,4,6 etc) or dropping every other row?
Sure. The remainder on dividing integers by 2 is either 1 or 0 depending on whether those integers are odd or even. In Stata with observation numbers _n this remainder
is simply
mod(_n,2)
Logical negation ! flips 0 and 1 the other way round.
Thus try
sysuse auto, clear list mpg if mod(_n,2) list mpg if !mod(_n,2)

and so forth.
Note that
1. you do not need to create any extra variables.
2. the technique generalises easily
to related problems: e.g. every 5th observation is selected by
if !mod(_n,5)
view that there are useful functions that
people persistently overlook, although their usefulness can be blindingly obvious
once pointed out. Sooner or later I will
write a Stata Journal Tip on -mod()-.

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

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