help drop, help keep tool: Variables Manager
dialog: keep/drop observations
-------------------------------------------------------------------------------
Title
[D] drop -- Eliminate variables or observations
Syntax
Drop variables
drop varlist
Drop observations
drop if exp
Drop a range of observations
drop in range [if exp]
Keep variables
keep varlist
Keep observations that satisfy specified condition
keep if exp
Keep a range of observations
keep in range [if exp]
by is allowed with the second syntax of drop and the second syntax of
keep; see [D] by.
Menu
Keep or drop variables
Data > Variables Manager
Keep or drop observations
Data > Create or change data > Keep or drop observations
Description
drop eliminates variables or observations from the data in memory.
keep works the same as drop, except that you specify the variables or
observations to be kept rather than the variables or observations to be
deleted.
Warning: drop and keep are not reversible. Once you have eliminated
observations, you cannot read them back in again. You would need to go
back to the original dataset and read it in again. Instead of applying
drop or keep for a subset analysis, consider using if or in to select
subsets temporarily. This is usually the best strategy. Alternatively,
applying preserve followed in due course by restore may be a good
approach.
Examples
Setup
. sysuse census
Describe the data
. describe
Drop all variables with names that begin with pop
. drop pop*
Describe the resulting data
. describe
Drop marriage and divorce
. drop marriage divorce
Describe the resulting data
. describe
Drop any observation for which medage is greater than 32
. drop if medage > 32
Drop the first observation for each region
. by region, sort: drop if _n == 1
Drop all but the last observation in each region
. by region: drop if _n != _N
Keep the first 2 observations in the dataset
. keep in 1/2
Describe the resulting data
. describe
Drop all observations and variables
. drop _all
Describe the resulting data
. describe
Also see
Manual: [D] drop
Help: [D] clear, [D] varmanage