File size: 9,509 Bytes
3fb2143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
* Set Directory
clear
set more off
set scheme s1mono

cd "$path"
global data_files "$path/Data"
global out_files "$path/output"

**==============================================================================
* Weather data
use "$data_files/Raw/weather_daily.dta", clear

collapse (sum) pre (mean) tem_mean, by(city_id year month)
save "$data_files/weather_monthly.dta", replace

use "$data_files/Raw/weather_daily.dta", clear
gen quarter = int((month-1)/3)+1

collapse (sum) pre (mean) tem_mean, by(city_id year quarter)
save "$data_files/weather_quarterly.dta", replace

use "$data_files/Raw/weather_daily.dta", clear
gen quarter = int((month-1)/3)+1

bys city_id year quarter: egen wd = mode(wdmax), max
keep city_id year quarter wd
duplicates drop
save "$data_files/wind_quarterly.dta", replace

**==============================================================================
* Firm-level: enforcement data
use "$data_files/Raw/firm_info.dta", clear

merge 1:m id using "$data_files/Raw/enf_info.dta"
keep if _merge == 3
drop _merge

merge m:1 city_id year quarter using "$data_files/weather_quarterly.dta"
keep if _merge == 3
drop _merge

merge m:1 city_id year quarter using "$data_files/wind_quarterly.dta"
keep if _merge == 3
drop _merge

capture drop ibear
program ibear
   
    args lat1 lon1 lat2 lon2 newvar
   
    tempname d2r r2d
    scalar `d2r' = _pi / 180
    scalar `r2d' = 180 / _pi

    gen `newvar' = atan2(sin((`lon2'-`lon1') * `d2r') * cos(`lat2' * `d2r') , ///
                            cos(`lat1' * `d2r') * sin(`lat2' * `d2r') - ///
                            sin(`lat1' * `d2r') * cos(`lat2' * `d2r') * ///
                            cos((`lon2'-`lon1') * `d2r'))
   
    // normalize atan2 results (-pi to pi) to range from 0 to 360 degrees
    replace `newvar' = mod((`newvar' * `r2d') + 360,3 60)
	
end

ibear monitor_lat monitor_lon lat lon angle
replace wd = 22.5*(wd-1)

gen upwd = 1 if angle - wd < 45 & angle - wd > -45
replace upwd = 1 if (angle - wd < 45 | angle - 360 - wd > -45) & wd <= 45
replace upwd = 1 if (angle + 360 - wd < 45 | angle - wd > -45) & wd >= 315
replace upwd = 0 if upwd == .

egen time = group(year quarter)
gen min_dist_10 = min_dist < 10
gen post1 = year >= 2015

gen air_1 = air==1
gen air_2 = air>=2

gen leni = (any_air_shutdown + any_air_fine + any_air_renovate == 1)
gen stri = (any_air_shutdown + any_air_fine + any_air_renovate == 3)

gen min_d_d4 = int(min_dist/5)
replace min_d_d4 = 4 if min_d_d4 > 4

bysort city_id: egen med_pre=median(pre)
gen high_pre=0 if pre!=.
replace high_pre=1 if pre!=. & pre>med_pre

save "$data_files/firm_enf.dta", replace

**==============================================================================
use "$data_files/firm_enf.dta", clear

gen any_air_10 = any_air & min_dist < 10
gen any_air_20 = any_air & min_dist < 50 & min_dist > 10
gen any_air_50 = any_air & min_dist > 50

collapse (sum) any_air any_air_*, by(city_id year quarter)
save "$data_files/enf.dta", replace

**==============================================================================
* Mayor data
use "$data_files/Raw/mayor.dta", clear
format %td start_date
format %td end_date
format %td birthdate

bysort city_cn city_id (start_date end_date): gen next_start=start_date[_n+1]
replace next_start=end_date if next_start==.
format %td next_start

foreach v of varlist start_date next_start end_date {
    gen `v'_m = mofd(`v')
	format %tm `v'_m
}

bysort city_cn city_id (start_date next_start_m): replace next_start_m=next_start_m-1 if _n!=_N
expand next_start_m-start_date_m + 1

by city_cn city_id name start_date (next_start_m), sort: gen month_date = start_date_m + _n - 1
format month_date %tm
gen month=month(dofm(month_date))
gen year=year(dofm(month_date))

sort city_cn city_id month_date name

destring city_id, replace
drop if city_id==.
save "$data_files/mayor_panel.dta", replace

keep if year==2015
format %td birthdate
gen birth_year=year(birthdate)
gen birth_month=month(birthdate)
gen age_2017=2018-birth_year
bysort city_id: egen age = mode(age_2017), maxmode
bysort city_id: egen age_month = mode(birth_month), maxmode

keep city_id city_cn age age_month
duplicates drop

replace age = age+1 if age_month==1 & age==57
drop age_month

save "$data_files/age_2017.dta", replace

use "$data_files/mayor_panel.dta", clear
format %td birthdate
gen birth_year=year(birthdate)
gen birth_month=month(birthdate)

gen age = year-birth_year
bys city_id year: egen age_year = mode(age)

keep if year >= 2010 & year <= 2017
keep city_id year age_year
duplicates drop

bys city_id (year): replace age_year = age_year[_n-1]+1 if age_year == .
bys city_id (year): replace age_year = age_year[_n+1]-1 if age_year == .
bys city_id (year): replace age_year = age_year[_n-1]+1 if age_year == .
bys city_id (year): replace age_year = age_year[_n+1]-1 if age_year == .

save "$data_files/age_year.dta", replace

**==============================================================================
* Monitor data
use "$data_files/raw/pm_pix.dta", clear

keep city_id p_id p_lon p_lat
duplicates drop

rename city_id city_id2
joinby city_id2 using "$data_files/Raw/monitor_city_long.dta"

geodist p_lat p_lon monitor_lat monitor_lon, gen(dist)
keep if dist < 20

expand 8
bys city_id monitor_id p_id: gen year = 2009 + _n
expand 12
bys city_id monitor_id p_id year: gen month = _n

merge m:1 p_id year month using "$data_files/raw/pm_pix.dta"
keep if _merge == 3
drop _merge

drop if pm25 == .
bys monitor_id year month (dist): keep if _n == 1

keep monitor_id city_id year month pm25 compare
save "$data_files/monitor_pix.dta", replace

keep if compare == 0
collapse (mean) pm25, by(city_id year month)
rename pm25 pm

save "$data_files/pix.dta", replace

**==============================================================================
* City-level: AOD data
use "$data_files/Raw/city_info.dta", clear

merge 1:m city_id using "$data_files/raw/pm.dta"
keep if _merge == 3
drop _merge

merge 1:1 city_id year month using "$data_files/weather_monthly.dta"
keep if _merge == 3
drop _merge

merge m:1 city_id using "$data_files/age_2017.dta"
keep if _merge == 3
drop _merge

merge m:1 city_id year using "$data_files/age_year.dta"
keep if _merge == 3
drop _merge

merge 1:1 city_id year month using "$data_files/Raw/lights.dta"
keep if _merge == 3
drop _merge

gen pred = int(pre/20)
gen tem_meand = int(tem_mean)

sort city_id year month
gen quarter = int((month-1)/3)+1
egen time = group(year quarter)

merge m:1 city_id year quarter using "$data_files/enf.dta", keepusing(any_air)
replace any_air = 0 if _merge == 1
drop _merge

gen post1 = year >= 2015

merge 1:1 city_id year month using "$data_files/pix.dta"
drop _merge

save "$data_files/city_pm.dta", replace

expand 2, gen(cutoff)
replace cutoff = cutoff + 1
save "$data_files/city_pm_rd.dta", replace

**==============================================================================
* City-level: Enforcement data
use "$data_files/Raw/city_info.dta", clear
expand 8
bys city_id: gen year = _n + 2009
expand 4
bys city_id year: gen quarter = _n

merge 1:1 city_id year quarter using "$data_files/weather_quarterly.dta"
drop _merge

merge 1:1 city_id year quarter using "$data_files/enf.dta"
drop _merge

merge m:1 city_id using "$data_files/age_2017.dta"
keep if _merge == 3
drop _merge

merge m:1 city_id year using "$data_files/age_year.dta"
keep if _merge == 3
drop _merge

gen pred = int(pre/20)
gen tem_meand = int(tem_mean)

sort city_id year quarter
egen time = group(year quarter)

replace any_air = 0 if any_air == .
gen log_any_air = log(any_air+1)

replace any_air_10 = 0 if any_air_10 == .
gen log_any_air_10 = log(any_air_10+1)
replace any_air_20 = 0 if any_air_20 == .
gen log_any_air_20 = log(any_air_20+1)

replace any_air_50 = 0 if any_air_50 == .
gen log_any_air_50 = log(any_air_50+1)

gen post1 = year >= 2015

merge 1:1 city_id year quarter using "$data_files/Raw/non-asif.dta"
drop _merge

save "$data_files/city_enf.dta", replace

expand 2, gen(cutoff)
replace cutoff = cutoff + 1
save "$data_files/city_enf_rd.dta", replace

**==============================================================================
use "$data_files/Raw/daily_monitor_api.dta", clear

collapse (mean) pm25api pm10api AQI, by(city_id monitor_id year month)
save "$data_files/monthly_api.dta", replace

**==============================================================================
use "$data_files/Raw/monitor_info.dta", clear

merge 1:m monitor_id using "$data_files/monitor_pix"
keep if _merge == 3
drop _merge

keep if year>=2015 & year<=2017

merge 1:1 monitor_id year month using "$data_files/monthly_api.dta"
keep if _merge == 3
drop _merge

merge m:1 city_id year month using "$data_files/weather_monthly.dta"
keep if _merge == 3
drop _merge

merge m:1 city_id year using "$data_files/age_year.dta"
keep if _merge == 3
drop _merge

save "$data_files/monitor_api.dta", replace

**==============================================================================
use "$data_files/Raw/firm_info.dta", clear

keep if key == 1
gen revenue_5 = revenue if min_dist < 5
gen revenue_10 = revenue if min_dist < 10

gen employment_5 = employment if min_dist < 5
gen employment_10 = employment if min_dist < 10

collapse (sum) revenue* employment*, by(city_id)

gen share_rev_10 = revenue_10/revenue
gen share_rev_5 = revenue_5/revenue

gen share_emp_10 = employment_10/employment
gen share_emp_5 = employment_5/employment

keep share* city_id
save "$data_files/share.dta", replace