In Stata, how do I create a new variable based on existing data?
To create a new variable in Stata, use the gen
command (short for "generate").
- The following example creates a new variable called
newvarand sets it equal to 0: gen newvar = 0 - The following example creates a new variable called
totalfrom the transformation of existing variables, the sum ofv1,v2,v3, andv4: gen total = v1 + v2 + v3 + v4Equivalently, use the built-in
egen total = rowtotal(v1 v2 v3 v4)rowtotaloption of theegencommand:Note that
egentreats missing values as0. - The following example creates a variable called
avg, which stores the average of the four variables: gen avg = (v1 + v2 + v3 + v4) / 4Note that the
/(slash) is used to denote division; a*(asterisk) would be used for multiplication.Equivalently, use the built-in
egen avg = rowmean(v1 v2 v3 v4)rowmeanoption of theegencommand: - Finally, you can take advantage of built-in functions for variable
transformations. The following example takes the natural log of
v1and creates a new variable calledv1_log: gen v1_log = log(v1)
For more on how to use functions and the gen and egen
commands, enter help functions, help
gen and help egen in Stata.
For information about where to find statistical and mathematical software manuals at Indiana University Bloomington and at IUPUI, see Software Manuals and Documents.
For more about statistical and mathematical software, email the UITS Stat/Math Center, visit the center's web page, or phone 812-855-4724 (IUB) or 317-278-4740 (IUPUI). The center is located in Bloomington at 410 N. Park Avenue, and is open for consultation by appointment Monday-Friday 9am-5pm.
Last modified on April 27, 2011.







