To create a new variable that is an algebraic expression of existing variables use the command "generate" (An acceptable short form of "generate" is "gen".)
To change the values of an existing variables as a function of its original values or the values of other variables, use the command "replace". (An acceptable short form of "replace" is "rep".)
Many operators and functions are permissible:
Basic operators:
+ addition
- subtraction
* multiplication
/ division
^ exponentiation (power)
Mathematical functions:
ln(x) natural logarithm of x
log(x) natural logarithm of x
log10(x) logarithm, base 10 of x
sqrt(x) square root of x
exp(x)
abs(x) absolute value of x
Logical operators:
> greater than
< less than
>= greater than or equal
<= less than or equal
== equal (double equal signs)
~= not equal
Examples of the "gen" command:
gen a = b + c
gen a = sqrt (b)
Extensions of the Generate Command - "egen"
"egen" incorporates a variety of special functions extending the capabilities of the "generate" command. Among those available: count, ma mean, median, rank, st, std, sum
The STATA command "recode" changes the values of a categorical variable into new values. Ranges can be specified by using the slash "/" operator.
Examples of the "recode" command:
recode a 1=2 2=1 This command interchanges the values of 1 and 2 for variable a.
recode a 2=1 3=2 4=2 This command collapses a four category variable into two categories
recode a min/2=1 3/max=2 Another way to recode 1,2 into 1 and 3, 4 into 2.
recode a 9=. Makes "9" into a missing value.