Using Stata for Survey Data Analysis


replace  This command is used to change the definition of an existing variable. The syntax is the same:  replace


Download 1.39 Mb.
Pdf ko'rish
bet28/61
Sana08.03.2023
Hajmi1.39 Mb.
#1252470
1   ...   24   25   26   27   28   29   30   31   ...   61
Bog'liq
2009 Usingstataforsurveydataanalysis (1)

replace 
This command is used to change the definition of an existing variable. The syntax is the same: 
replace oldvar = expression [if exp] [in exp] 
Some points to remember: 
Replace cannot be used to create a new variable. Stata will give an error message if the 
variable does not exist. 
There is no abbreviation for “replace.” Stata wants to make sure you really want to change it. 


Using Stata for Survey Data Analysis 
 
Minot 
 
Page 23
If you use the “if” option, then the old values will be retained when the “if” statement is false
You can use the period (.) to represent missing values 
For example, 
replace price = avgprice if price > 100000 
replaces high values with an average price 
replace pcexpend =. if pcexpend<=0
replace negative pcexpend with missing value 
replace agehead = 25 in 1007
replace age=25 in observation #1007 
Example 8 shows the use of the gen and replace commands to create a new variable called region.
The new variables has three values: 1 for west, 2 for center, and 3 for east. 
Example 8: Using “generate” and “replace” to create new variables 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tabulate … generate
This command is useful for creating a set of dummy variables (variables with a value of 0 or 1) 
depending on the value of an existing categorical variable. The syntax is: 
tabulate oldvariable, generate(newvariable
It is easier to explain with an example. Suppose we want to create three dummy variables that 
indicate whether a household is in the west, center, or east of Bhutan. We can create three dummy 
variables from the variable “region” as follows:
tab region, gen(reg) 
This creates three new variables, defined as follows:
reg1=1 if region=1 and 0 otherwise 
reg2=1 if region=2 and 0 otherwise 
reg3=1 if region=3 and 0 otherwise
In the example below, notice that there are 1746 households in region 1 (west) and the same number 
of households for which reg1 = 1. 



Download 1.39 Mb.

Do'stlaringiz bilan baham:
1   ...   24   25   26   27   28   29   30   31   ...   61




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling