How To Add Up Variables In Stata
The master method for creating new variables in Stata is the generate
command. Load the auto
dataset.
clear sysuse car describe
New Variable from Existing Variables
Allow's create a new variable that is the sum of weight
and length
(ignore for the moment that summing weights and lengths doesn't make a ton of sense). The syntax of generate
is:
generate nameOfNewVariable = whateverTheNewVariableIsEqualTo
So to create a new variable called weightlength
that is the sum of weight
and length
we type:
generate weightlength = weight+length
At present we take new variable called weightlength
.
Suppose now that we want to create a new variable that is the foursquare of weight.
generate weight2 = weight^2
New Variable that is a Constant
Suppose nosotros want to create a new variable that is a abiding value (this isn't necessarily a practiced thought and you lot can use macros to store constants but using a variable can exist pretty convenient too). Allow'due south brand a new variable x
that is equal to 100.
generate x = 100
Let's create a new variable that is equal to the mean of weight — we'll call information technology meanweight
.
summarize weight
generate meanweight = 3019.459
You can likewise use the results of the summarize
command to create a mean.
summarize weight generate meanweight = r(hateful)
You tin use the _N
operator to create a new variable that is equal to the number of observations in a dataset.
generate obs = _N
If you combine this with by
you lot tin create a new variable that will be equal to the number of observations inside the levels of the by
variable. For example, we can type:
by foreign: generate obs = _N
This will create a variable that is a constant inside the levels of foreign
. That is, we are going to go the number of strange cars and the number of domestic cars. If a line in the information is associated with foreign cars the new obs
variable volition have a value of 22 and domestic cars volition take a value of 52. Give it a try and see how it works.
New Variable that is a Random Draw from a Distribution
Nosotros tin can create a new variable that is a random draw from a distribution. Let's create a new variable whose values will be random draws from a normal distribution with a mean of 0 and a standard deviation of 1. The random normal generator command is rnormal()
(it defaults to a mean of 0 and standard deviation of 1 and information technology will depict as many values as there are observations in the dataset).
generate random = rnormal()
Create a New Variable that Indexes the Observations
You can utilise the _n
operator to create a variable that indexes the observation number.
generate index = _n
This will create a new variable that runs from 1 to 74. Y'all can combine this with by
to create an index within another variable.
by foreign: alphabetize = _n
This will create a new variable that runs from 1 to 52 for domestic cars and 1 to 22 for foreign cars.
Conclusion
I've just touched on the ways yous can create new variables. Y'all tin can also use the egen
command to create new variables. Try new means to create variables and be certain to read the help files.
How To Add Up Variables In Stata,
Source: https://psychstatistics.com/2020/07/27/stata-using-generate-to-create-new-variables/
Posted by: baileyolonstake.blogspot.com
0 Response to "How To Add Up Variables In Stata"
Post a Comment