Basic variable generation
Basic variable generation
sysuse
census.dta, clear
* Generate urban share
generate
urban_share = popurban/pop // generate variable "urban_share" as ratio popurban to pop
summarize
urban_share, detail // display summary statistics to check
* Generate urban percentile
gen
urban_perc = urban_share*100
sum
urban_perc, detail
* Generate log urban share
gen
ln_urban_share = ln(urban_share) // you can also use functions
sum
ln_urban_share, detail
help
ln() // add "()" to access help-file of functions
help
function // overview of all functions
* Replace values
// replacing works the same as generating variables
replace
urban_share = urban_share * 100 // replace values of urban_share by its product with 100
* Missing values
gen
urban_share_1 = urban_share if region==1 // urban_share for obs. of region 1
br
state region urban_share* // urban_share_1 is missing for all other regions
replace
urban_share = . if region==1 // set urban_share to missing for region 1
replace
urban_share = .a if region==1 // set urban_share to special missing for region 1
help
missing
* String variables
gen
country = "USA" // strings need to be put in quotes
gen
region_str = "" // missing value for strings: ""
replace
region_str = "North East" if region==1 // fill string variable with region names
replace
region_str = "North Central" if region==2
replace
region_str = "South" if region==3
replace
region_str = "West" if region==4
// for more, see section on string variables
* Copy variables
gen
region1 = region // Variant 1: simply defines new variable by content of old variable
clonevar
region2 = region // Variant 2: also copies the labels
br
region*
* Order variables
gen
no1 = 1, before(state) // generates new variable & puts it before variable "state"
gen
no2 = 2, after(state) // generates new variable & puts it after variable "state"
Exercise
Load the pre-installed dataset auto.
- The variables “headroom” and “length” are given in inches. Create two new variables which contain the headroom size and the length in cm (1 inch = 2.54 cm).
- Replace the variable “gear_ratio” by gear ratio rounded to the next integer (you need to look for the correct function for this).
- Clone the variable “make”. Replace the values of the new variable such that the name of all foreign cars is now “Foreign”.