[Stata] Data cleaning 1: Managing variables (rename, order, keep, and drop)

First, you can see the list of variables in the data in the variable browser on the upper right side of STATA screen.

The command codebook, compact also return the list of variable, its # of observations, # of unique values, minimum, maximum, and variable label.

Stata
codebook, compact 

Managing variables

rename command: changes the variable name

Stata
rename old_name new_name 

order command: order relocates varlist to a position depending on which option you specify. If no option is specified, order relocates varlist to the beginning of the dataset in the order in which the variables are specified.

Stata
order v1: Move v1 to the beginning of the dataset
order v1, last: Move v1 to the end of the dataset
order v3, before(v3): Move v3 before v2
order x z, after(y): Move x and z after y

Dropping variables in the dataset

Stata
* drop command: drops the variable from the dataset 
drop var_name

* keep command: drops all variables except for the variable (var_name)
keep var_name
  • June 1, 2023