//indicating the path and working folder cd "C:\Users\Aki\Documents\stata" log using feb7 ******Exploring data part 2. Data manipulation******************** ***DISTRIBUTIONAL ANALYSIS***** sysuse auto, clear su price su price, detail inspect price **formal test for normality sktest price //Shapiro-Wilk test for normality //H0: the distribution is normal //With p-value close to 0, we are rejecting the H0: the distribution is NOT normal sktest length //at 5% significance level, we fail to reject H0 (the distribution is normal) //at 1% level, we reject H0 (the distribution is NOT normal) su length, detail ********DATA MANIPULATION************ ** 1. RECODING******* tab rep78 //want to merge categories 1 and 2 together, to have 4 categories instead of 5 categories for the variable rep78 help recode recode rep78 1 2 = 1 tab rep78 sysuse auto, clear recode rep78 (3=2) (4=3) (5=4) tab rep78 tab mpg recode mpg (12/19 = 1) (20/29 = 2) (30/41 = 3), gen(mpgcat) tab mpgcat //can be helpful to create a categorical variable out of descrete variable ** 2. GENERATING A NEW VARIABLE****** help gen **generate a constant gen constant = 1 **generating a new variable using some old variable //new price variable which is price divided by the weight of a car gen newprice = price / weight gen pricesquared = price*price gen pricesquared1 = price^2 gen price1000 = price + 1000 help functions gen ln_price = log(price) ***replace command**** sysuse auto, clear tab rep78 //want to merge categories 1 and 2, and have 4 categories instead of 5 gen rep78recoded = 1 //if rep78= 1, you don't want to change anything, rep78recoded = 1 //if rep78=2, you don't want to change anything, rep78recoded = 1 replace rep78recoded = 2 if rep78==3 replace rep78recoded = 3 if rep78==4 replace rep78recoded = 4 if rep78==5 tab rep78recoded //!!! be careful with missing values replace rep78recoded = . if rep78 ==. **!! use replace together with generate command*** //unlike recode command, in replace you can use logical and mathematical functions translate feb7.smcl feb7.log log close