Liz's Stata Guide

egen

Creates a new variable using various nifty functions. It cannot be abbreviated.

Typical Use:
egen bigvar = rmax(varx vary varz)

Example

To get a breakdown of EAs by district, we would use tag so that we can restrict ourselves to one observation per EA.

. egen ea_tag = tag(ea_code);

. tab district if ea_tag == 1;

          district |      Freq.     Percent        Cum.
-------------------+-----------------------------------
          Kailahun |         22       10.00       10.00
            Kenema |         28       12.73       22.73
              Kono |         13        5.91       28.64
           Bombali |         16        7.27       35.91
            Kambia |          9        4.09       40.00
         Koinadugu |         12        5.45       45.45
         Port Loko |         16        7.27       52.73
         Tonkolili |         12        5.45       58.18
                Bo |         22       10.00       68.18
            Bonthe |          4        1.82       70.00
           Moyamba |         11        5.00       75.00
           Pujehun |          8        3.64       78.64
Western Area Rural |         12        5.45       84.09
Western Area Urban |         35       15.91      100.00
-------------------+-----------------------------------
             Total |        220      100.00

Options

There are many different functions, these are the ones I use most often. See the official Stata help for a complete list.
diff(varlist) equals 1 for observations where the variables are not all equal, 0 if they are
eqany(varlist), v(numlist) equals 1 for observations where any of the variables in varlist are equal to any of the values in numlist.
neqany(varlist), v(numlist) equals number of variables in varlist equal to any of the values in numlist.
rank(exp)gives the rank of the observation based on exp where the highest value corresponds to 1. Equal observations are assigned the average rank.
rmax(varlist)
rmin(varlist)
rmean(varlist)
equals the maximum (minimum, mean) of the variables in varlist
robs(varlist)for each observation, equals the number of variables among varlist that aren't missing.
tag(varlist)equals 1 for 1 observation of each distinct group defined by varlist

Notes

contact: djiboliz@gmail.com
last modified: 23 Nov 2007