Skip to main content

Data Analysis with Stata


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

use "", 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.