Using Stata for Survey Data Analysis
Minot
Page 25
In Example 10, we want to know which households have per capita expenditure (pc_t_mo) above the
dzongkhag average. First, we calculate the average expenditure for each villaged with the “egen”
command. Then we create a dummy variable based on the expression (pc_t_mo > avgexp). The list
output shows how the dzongkhag average is repeated for every household and confirms that the
dummy variable is correctly calculated.
Example 10: Using “egen” to calculate averages
Using operators
Operators are symbols used in equations. Most of the operators are obvious (e.g. + and -), but some
are not.
Table 6 lists the most commonly used operators. They are similar to the operators in
SPSS, except that in Stata you cannot use words like “or”, “and”, “eq”, or “gt”.
Table 6. Key operators for writing equations in Stata
Operator Meaning
Example
+
addition
gen income = agincome + nonagincome
-
subtraction
gen netrevenue = revenue – cost
*
multiplication
gen value = price * quantity
/
division
gen exppc = expenditure/hhsize
^
power
gen agesquared = age^2
>
greater than
gen aboveavg = 1 if income > avgincome
<
less than
gen belowavg = 1 if income < avgincome
>=
more than or equal
gen child = 1 if age <=10
<=
less than or equal
gen adult = 1 if age >= 18
=
equal (to set value)
gen expend = foodexp + nonfoodexp
==
equal (in “if” condition) gen femhead = 1 if sexhead==2
~=
not equal
gen error = 1 if value1 ~= value2
!=
not equal
gen error = 1 if value1 != value2
|
or
gen age=. if age==999 | age=9999
&
and
gen sexhead = 1 if sex==1 & relation==1
The most difficult rule to remember is when to use = and when to use ==.
Use a single equal symbol (=) when defining a variable.
Use a double equal symbol (==) when you are testing an equality, such as in an “if” statement
and when creating a dummy variable.
Here is a short-cut for creating dummy variables. Suppose you want you create a dummy variable
indicating households in Paro. One way is to write:
generate Paro = 0
Do'stlaringiz bilan baham: |