1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
cd "C:/Users/wugan/Desktop/教对象Stata系列/第2课:变量管理与IF条件使用"
webuse renamexmpl, clear
gen X=. replace X=2
replace X=X+1 replace X=X-1 replace X=X*3 replace X=X/3 replace X=ln(X) replace X=log10(X) replace X=3^X replace X=exp(X) replace X=X^2 rename inc income
gen inc_ceil=ceil(income) gen inc_floor=floor(income) gen inc_int=int(income) gen inc_round=round(income) gen inc_round=round(income,.01) gen inc_round=round(income,7)
gen inc_mod=mod(income,2)
gen X1="" replace X1="NA" gen X2="缺失值" gen Y2=income+X2 gen Y=X1+X2
use "气象数据.dta", clear
gen provincename=usubstr(province,1,2) gen birthyear=usubstr(ID,7,4) gen region_type=usubstr(province,-1,.)
egen ProvinceGroup=group(province year)
egen city_num=count(city), by(province year)
egen wind_total=total(平均风速), by(province year) egen wind_mean =mean(平均风速) , by(province year) egen wind_sd =sd(平均风速) , by(province year) egen wind_max =max(平均风速) , by(province year) egen wind_min =min(平均风速) , by(province year)
egen address1=concat(province city) egen address2=concat(province city), punct(" ") egen address3=concat(province city), punct(,)
egen wind_p50=median(平均风速), by(province year) egen wind_p30=pctile(平均风速), by(province year) p(30)
egen list=seq(),from(1) to(3) by(province year)
egen list2=seq(), to(100000) by(province year)
egen wind_rtotal=rowtotal(平均气温 平均风速 累积降水 累积日照 平均湿度) egen wind_rmean =rowmean(平均气温 平均风速 累积降水 累积日照 平均湿度) egen wind_rsd =rowsd(平均气温 平均风速 累积降水 累积日照 平均湿度) egen wind_rmax =rowmax(平均气温 平均风速 累积降水 累积日照 平均湿度) egen wind_rmin =rowmin(平均气温 平均风速 累积降水 累积日照 平均湿度)
ssc install ereplace, replace
gen type=1 if 累积降水>=0 & 累积降水<200 replace type=2 if 累积降水>=200 & 累积降水<400 replace type=3 if 累积降水>=400 & 累积降水<800 replace type=4 if 累积降水>=800 & 累积降水!=.
gen select=1 if type==2 | type==4
replace type=4 if inrange(累积降水,800,9000000) replace select=1 if inlist(type,2,4) gen region=1 if inlist(province,"上海市","浙江省","江苏省") replace region=1 if strmatch(province,"*上海*") | strmatch(province,"*浙江*") | strmatch(province,"*江苏*") replace region=1 if province=="山东省"
webuse renamexmpl,clear
rename famid familycode
rename (sex inc) (gender income) gen Var1=. gen Var2=. rename Var# =new rename Var* V*
d gender label dir label list sex
{ label var gender "性别" label var income "收入" label var familycode "家庭编码" }
label drop sex label values gender
label define gender 1 "男" 2 "女", add label values gender gender
order familycode gender order promo, after(familycode) order promo, before(gender) order V*new, after(gender)
sort familycode sort familycode gender gsort + familycode gender gsort - familycode + gender
drop V*new keep familycode edu gender income
keep if edu>0 drop if familycode>100
ereplace income=total(income), by(familycode) duplicates drop familycode, force
export excel "演示数据.xlsx",sheet("Sheet1") firstrow(var) replace
|