[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: dropping even observations
The following question and reply arose privately.
I have been asked this various times before, so the
discussion should be of some wider interest.
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
Logical negation ! flips 0 and 1 the
other way round.
sysuse auto, clear
list mpg if mod(_n,2)
list mpg if !mod(_n,2)
and so forth.
1. you do not need to create any extra
2. the technique generalises easily
to related problems: e.g. every 5th
observation is selected by
Your query adds support to my longstanding
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: