Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

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 newvar and sets it equal to 0: gen newvar = 0
  • The following example creates a new variable called total from the transformation of existing variables, the sum of v1, v2, v3, and v4: gen total = v1 + v2 + v3 + v4

    Equivalently, use the built-in rowtotal option of the egen command:

    egen total = rowtotal(v1 v2 v3 v4)

    Note that egen treats missing values as 0.

  • The following example creates a variable called avg, which stores the average of the four variables: gen avg = (v1 + v2 + v3 + v4) / 4

    Note that the  /  (slash) is used to denote division; a  *  (asterisk) would be used for multiplication.

    Equivalently, use the built-in rowmean option of the egen command:

    egen avg = rowmean(v1 v2 v3 v4)
  • Finally, you can take advantage of built-in functions for variable transformations. The following example takes the natural log of v1 and creates a new variable called v1_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.

This is document afrg in domain all.
Last modified on April 27, 2011.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.