cd D:\Stata log using week2.txt,text replace sysuse auto,clear ************************************************ *** PART 1: command syntax illustrated with list ************************************************ list help list // using varlist list make mpg price l make mpg price list m* li price-weight list ma?e l gear_r~o // using if qualifier list if mpg>22 list if (mpg>22) & !missing(mpg) list make mpg price gear if (mpg>22) | (price>8000 & gear<3.5) list if make=="Datsun 510" list if foreign==0 // The following are the common mistakes. Can you see why? list if mpg=21 list if mpg==21 if weight>4000 list if mpg==21 and weight>4000 list if make==Datsun 510 // using in qualifier list in 1 list in -1 list in 2/4 list in -3/-2 // using options list ma p g f, sepby(foreign) list make weight gear, abbreviate(3) list,divider ************************************************** *** PART 2: basic data management ************************************************** // save and open datasets save autodata,replace use autodata,clear // variable names and labels preserve * rename the make variable --- Stata is case sensitive rename make Make * label variable gen domestic=1-foreign label variable domestic "Domestic or not" label define lbdomestic 0 "Foreign" 1 "Domestic" label values domestic lbdomestic list Make foreign domestic restore // Create new variables preserve list make mpg weight * change MPG to liters per 100km: generate lphk = 3.7854 * (100/1.6093) / mpg * getting logarithms of price: gen lnprice=ln(price) * making an indicator of hugeness gen huge=(weight>=300) if !missing(weight) * list the new variables l make mpg weight lphk lnprice huge * Want to use weight in 1000s of lbs, but this will cause a mistake: gen weight=weight/1000 * replace weight in lbs by weight in 1000s of lbs replace weight=weight/1000 * view the results list make weight * Also can generate with if qualifiers * For example, assume expecting price to increase differently by foreign gen predprice=1.05*price if foreign==0 replace predprice=1.10*price if foreign==1 list make foreign price predprice,nolabel * alternatively, can do this with one command: gen predprice2=(1.05+0.05*foreign)*price list make foreign price predprice predprice2, nolabel * Working with string variables gen where="D" if foreign==0 replace where="F" if foreign==1 list make foreign where describe where * Use string functions: gen model = substr(make,strpos(make, " ")+1,.) gen modelwhere=model+ " " + where list make where model modelwhere restore // Deleting variables and observations preserve list drop in 1/50 list drop if mpg>21 list drop gear_ratio list drop m* list restore preserve list keep in 40/70 list keep if mpg<=21 list keep m* list clear restore // Close the log files before you go log close