Skip to Main Content

Data Analysis with Stata

Examples

The dataset stock.dta has longitudinal data on (fictional) annual returns for a bunch of stocks. Find the highest return for each stock.

use "https://www3.nd.edu/~jng2/libguide/stocks.dta", clear
bysort stockid: egen maxreturn = max(return)

For each stockid, find the year/s that yielded the highest return.

Count the number of observations for each stockid.

bysort stockid: egen numobs = count(stockid)

(You can accomplish the same thing with tabulate stockid or duplicates report stockid.)

egen Suite of Commands

The egen command consists of functions that extend the capability of the generate command. The various functions within egen create variables that hold information about patterns and calculations within subgroups or across columns.