What's the difference between gen and egen in Stata

(Comments)

Is there a reason why there are two different commands to generate a new variable?

Is there a simple way to remember when to use gen and when to use egen?

gen

generate may be abbreviated by gen or even g and can be used with the following mathematical operators and functions:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • ^ power

A large number of functions is available. Here are some examples:

  • abs(x) absolute value of x
  • exp(x) antilog of x
  • int(x) or trunc(x) truncation to integer value
  • ln(x), log(x) natural logarithm of x
  • round(x) rounds to the nearest integer of x
  • round(x,y) x rounded in units of y (i.e., round(x,.1) rounds to one decimal place)
  • sqrt(x)square root of x
  • runiform() returns uniformly distributed numbers between 0 and nearly 1
  • rnormal() returns numbers that follow a standard normal distribution
  • rnormal(x,y) returns numbers that follow a normal distribution with a mean of x and a s.d. of y

egen

A number of more complex possibilities have been implemented in the egen command like in the following examples:

  • egen nkids = anycount(pers1 pers2 pers3 pers4 pers5), value(1)
  • egen v323r = rank(v323)
  • egen myindex = rowmean(var15 var17 var18 var20 var23)
  • egen nmiss = rowmiss(x1-x10 var15-var23)
  • egen nmiss = rowtotal(x1-x10 var15-var23)
  • egen incomst = std(income)
  • bysort v3: egen mincome = mean(income)

Detailed usage explanations can be found at this link.

Current rating: 5

Comments

Riddles

22nd Jul- 2020, by: Editor in Chief
524 Shares 4 Comments
Generic placeholder image
20 Oct- 2019, by: Editor in Chief
524 Shares 4 Comments
Generic placeholder image
20Aug- 2019, by: Editor in Chief
524 Shares 4 Comments
10Aug- 2019, by: Editor in Chief
424 Shares 4 Comments
Generic placeholder image
10Aug- 2015, by: Editor in Chief
424 Shares 4 Comments

More News  »

Why being minimalist good for your money

Recent news

Why being the minimalist is good for your money

read more
2 weeks, 6 days ago

a story which relates the events of your life from past, present and future to the life cycle of a star

Recent news

What will ChatGPT answer if thre is a question? Write a story which relates the events of your life from past, present and future to the life cycle of a star. Use also those elements which could symbolize something or anything in your story.​

read more
1 month ago

Still about chatGPT and how it can pass the school exam

Recent news

ChatGPT passes exams from law and business schools

ChatGPT is trained on vast amounts of online data in order to generate responses to user prompts. While it has gained traction among users, it has also raised some concerns about inaccuracies and its potential to perpetuate biases and spread misinformation.

read more
1 month, 3 weeks ago

How to get predicted value from Stata

Recent news

How to Obtain Predicted Values and Residuals in Stata


Linear regression is a method we can use to understand the relationship between one or more explanatory variables and a response variable.

read more
1 month, 3 weeks ago

List of actor and actrees that left hollywood for the daily job

Recent news

These Former Stars Quit Hollywood for Regular Day Jobs

Stacey Dash ©Eugene Powers/Shutterstock.comStacey Dash ©Eugene Powers/Shutterstock.com

While some celebrities enjoy fame and fortune, others prefer to take the cash and then return to their normal lives, or pursue a different dream. Whether this departure from the limelight is temporary or for good, these former A-listers are now working day jobs that more closely resemble our own.

read more
1 month, 3 weeks ago

How do I remove a specific value from an array?

Recent news

How do I remove a specific value from an array? Something like:

read more
1 month, 3 weeks ago

Meet the real figure of Viking season 2 Valhalla

Recent news

Meet the real historical figures of Vikings Valhalla season 2

The historical drama is back with some new faces from the Viking Age.

The series was inspired by mythology and Viking sagas and is set 100 years after the events of the original Amazon Prime Video series.

read more
1 month, 4 weeks ago

What's the difference between gen and egen in Stata

Recent news

Is there a reason why there are two different commands to generate a new variable?

read more
2 months ago

More News »

Generic placeholder image

Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without