Virata manual

codebook

Goes through variables one at a time and says what is really in them.

What we have

  • how many distinct values, how many missing, and the range
  • the values themselves, with their labels, for a short list

Coming later

  • compact — accepted, but the full form is printed anyway
  • inspect, Stata's quick look at a variable's values

How you write it

codebook [varlist]

Square brackets mean optional. A vertical bar means choose one.

What it does

Where describe tells you what a column is *declared* to be, codebook tells you what it actually contains: how many different values there are, how many are blank (missing), the range, and — when there are only a few — the values themselves with their labels.

This is how you find the column that is supposed to be 1 and 2 but has a stray 9, or the "age" that runs to 999.

Try it

Every example below was run after sysuse vira.sample02, clear.

. codebook sector
------------------------------------------------------------------------------
sector  Sector: 1 rural 2 urban
------------------------------------------------------------------------------
             type:  numeric (int64)
            range:  [1, 2]      units: 1
    unique values:  2          missing: 0/6,000

       tabulation:  Freq.  Value
                    3,987  Rural
                    2,013  Urban
The same thing in pandas and in R

R

library(dplyr)
df %>% count(sector)
summary(df$sector)

# This R has NOT been run against a real R yet: read it as documentation.

Nearby

describe list count misstable duplicates assert