-
Notifications
You must be signed in to change notification settings - Fork 0
/
nd2020a.do
602 lines (452 loc) · 19.5 KB
/
nd2020a.do
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
////////////////////////////////////////
/*
Kevin DeLuca
Clean ND precinct data 2020 elections
2/23/21
*/
////////////////////////////////////////
clear
set more off
*clean each file, need to go through each county page in each spreadsheet for each
*first, turn each spreadsheet and all the tabs inside into usable stata data:
/*
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/PRESIDENT/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/PRESIDENT/Statewide Precinct Results.xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/USHOUSE/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/USHOUSE/Statewide Precinct Results(1).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/GOV/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/GOV/Statewide Precinct Results(2).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AUDITOR/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AUDITOR/Statewide Precinct Results(3).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/TREASURER/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/TREASURER/Statewide Precinct Results(4).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/INSCOM/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/INSCOM/Statewide Precinct Results(5).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/PSC/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/PSC/Statewide Precinct Results(6).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/SUPTPI/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/SUPTPI/Statewide Precinct Results(7).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/SUPCRT/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/SUPCRT/Statewide Precinct Results(8).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AMEND1/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AMEND1/Statewide Precinct Results(9).xlsx"
xls2dta , allsheets save("/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AMEND2/") : import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AMEND2/Statewide Precinct Results(10).xlsx"
*/
*clean state leg dataverse
* I think for each spreadsheet, go through every county name to see if a sheet with that county name is there - that gives you countyname and precincts, for the candidates specific to the race, which are generally in the same format
*note: when I export the speadsheet version of the precinct results all the votes are "0"s, so I don't think it will work. Here is some code I started working on...
// global counties = `" "Adams" "Barnes" "Benson" "Billings" "Bottineau" "Bowman" "Burke" "Burleigh" "Cass" "Cavalier" "Dickey" "Divide" "Dunn" "Eddy" "Emmons" "Foster" "Golden Valley" "Grand Forks" "Grant" "Griggs" "Hettinger" "Kidder" "LaMoure" "Logan" "McHenry" "McIntosh" "McKenzie" "McLean" "Mercer" "Morton" "Mountrail" "Nelson" "Oliver" "Pembina" "Pierce" "Ramsey" "Ransom" "Renville" "Richland" "Rolette" "Sargent" "Sheridan" "Sioux" "Slope" "Stark" "Steele" "Stutsman" "Towner" "Traill" "Walsh" "Ward" "Wells" "Williams" "'
// foreach var in $counties{
// capture import excel "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/STATELEG/sen2.xlsx", clear sheet("`var'"")
// tempfile `var'
// save ``var''
// }
// foreach var in $counties{
// capture append using ``var''
// }
// STOP
// compress
// save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/stateleg.dta", replace
*cleaning non-state leg data
*PRESIDENT
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/PRESIDENT/Statewide Precinct Results_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
rename E candidate3
rename F candidate4
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Trump and Pence" if num==1
replace candidate = "Biden and Harris" if num==2
replace candidate = "Jorgensen and Cohen" if num==3
replace candidate = "WRITEIN" if num==4
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
replace party_detailed = "REPUBLICAN" if candidate=="Trump and Pence"
replace party_detailed = "DEMOCRATIC-NPL" if candidate=="Biden and Harris"
replace party_detailed = "LIBERTARIAN" if candidate=="Jorgensen and Cohen"
gen office="PRESIDENT"
gen dataverse="PRESIDENT"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/president.dta", replace
*US HOUSE
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/USHOUSE/Statewide Precinct Results(1)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
rename E candidate3
rename F candidate4
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Kelly Armstrong" if num==1
replace candidate = "Zach Raknerud" if num==2
replace candidate = "Steven James Peterson" if num==3
replace candidate = "WRITEIN" if num==4
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
replace party_detailed = "REPUBLICAN" if candidate=="Kelly Armstrong"
replace party_detailed = "DEMOCRATIC-NPL" if candidate=="Zach Raknerud"
replace party_detailed = "LIBERTARIAN" if candidate=="Steven James Peterson"
gen office="US HOUSE"
gen district="000"
gen dataverse="HOUSE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/ushouse.dta", replace
*GOVERNOR
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/GOV/Statewide Precinct Results(2)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
rename E candidate3
rename F candidate4
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Doug Burgum and Brent Sanford" if num==1
replace candidate = "Shelley Lenz and Ben Vig" if num==2
replace candidate = "DuWayne Hendrickson and Joshua Voytek" if num==3
replace candidate = "WRITEIN" if num==4
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
replace party_detailed = "REPUBLICAN" if candidate=="Doug Burgum and Brent Sanford"
replace party_detailed = "DEMOCRATIC-NPL" if candidate=="Shelley Lenz and Ben Vig"
replace party_detailed = "LIBERTARIAN" if candidate=="DuWayne Hendrickson and Joshua Voytek"
gen office="GOVERNOR"
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/governor.dta", replace
*STATE AUDITOR
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AUDITOR/Statewide Precinct Results(3)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
rename E candidate3
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Joshua C Gallion" if num==1
replace candidate = "Patrick Hart" if num==2
replace candidate = "WRITEIN" if num==3
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
replace party_detailed = "REPUBLICAN" if candidate=="Joshua C Gallion"
replace party_detailed = "DEMOCRATIC-NPL" if candidate=="Patrick Hart"
gen office="STATE AUDITOR"
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/auditor.dta", replace
*STATE TREASURER
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/TREASURER/Statewide Precinct Results(4)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
rename E candidate3
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Thomas Beadle" if num==1
replace candidate = "Mark Haugen" if num==2
replace candidate = "WRITEIN" if num==3
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
replace party_detailed = "REPUBLICAN" if candidate=="Thomas Beadle"
replace party_detailed = "DEMOCRATIC-NPL" if candidate=="Mark Haugen"
gen office="STATE TREASURER"
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/treasurer.dta", replace
*INSURANCE COMMISSIONER
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/INSCOM/Statewide Precinct Results(5)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Jon Godfread" if num==1
replace candidate = "WRITEIN" if num==2
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
replace party_detailed = "REPUBLICAN" if candidate=="Jon Godfread"
gen office="INSURANCE COMMISSIONER"
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/inscom.dta", replace
*PUBLIC SERVICE COMMISSIONER
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/PSC/Statewide Precinct Results(6)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
rename E candidate3
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Brian Kroshus" if num==1
replace candidate = "Casey D Buchmann" if num==2
replace candidate = "WRITEIN" if num==3
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
replace party_detailed = "REPUBLICAN" if candidate=="Brian Kroshus"
replace party_detailed = "DEMOCRATIC-NPL" if candidate=="Casey D Buchmann"
gen office="PUBLIC SERVICE COMMISSIONER"
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/psc.dta", replace
*SUPERINTENDENT OF PUBLIC INSTRUCTION
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/SUPTPI/Statewide Precinct Results(7)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
rename E candidate3
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Kirsten Baesler" if num==1
replace candidate = "Brandt J Dick" if num==2
replace candidate = "WRITEIN" if num==3
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = "NONPARTISAN"
gen office="SUPERINTENDENT OF PUBLIC INSTRUCTION"
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/suptpi.dta", replace
*SUPREME COURT JUSTICE
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/SUPCRT/Statewide Precinct Results(8)_`x'.dta", clear
rename A precinct
rename B candidate1
rename C candidate2
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "Jon Jay Jensen" if num==1
replace candidate = "WRITEIN" if num==2
destring(votes), replace
drop if votes==.
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = "NONPARTISAN"
gen office="SUPREME COURT JUSTICE"
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/supcrt.dta", replace
*MEASURE 1
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AMEND1/Statewide Precinct Results(9)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "YES" if num==1
replace candidate = "NO" if num==2
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
gen office="Constitutional Measure No. 1 Relating to the state board of higher education"
replace office=strupper(office)
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/amend1.dta", replace
*MEASURE 2
forval x=1(1)53{
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/AMEND2/Statewide Precinct Results(10)_`x'.dta", clear
rename B precinct
rename C candidate1
rename D candidate2
drop A
keep if precinct!=""
drop if precinct=="TOTALS"|precinct=="Precinct"
reshape long candidate, i(precinct) j(num)
rename candidate votes
gen candidate = ""
replace candidate = "YES" if num==1
replace candidate = "NO" if num==2
destring(votes), replace
drop num
gen countynum = `x'
tempfile temp`x'
save `temp`x''
}
use `temp1', clear
forval x=2(1)53{
append using `temp`x''
}
gen party_detailed = ""
gen office="Constitutional Measure No. 2 Relating to initiated constitutional amendments."
replace office=strupper(office)
gen dataverse="STATE"
gen special="FALSE"
compress
save "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/amend2.dta", replace
use "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/president.dta", clear
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/ushouse.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/governor.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/auditor.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/treasurer.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/inscom.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/psc.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/suptpi.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/supcrt.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/amend1.dta"
append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/amend2.dta"
//append using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/raw/stateleg.dta"
gen party_simplified = ""
replace party_simplified = "REPUBLICAN" if party_detailed=="REPUBLICAN"
replace party_simplified = "DEMOCRATIC" if party_detailed=="DEMOCRATIC-NPL"
replace party_simplified = "LIBERTARIAN" if party_detailed=="LIBERTARIAN"
replace party_simplified = "NONPARTISAN" if party_detailed=="NONPARTISAN"
gen writein="FALSE"
replace writein="TRUE" if candidate=="WRITEIN"
gen mode = "TOTAL"
replace candidate = strupper(candidate)
replace candidate = subinstr(candidate," "," ",.)
replace district = "STATEWIDE" if office!="US HOUSE"&office!="PRESIDENT"
tab office district
merge m:1 countynum using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/countynumxwalk.dta", nogen keep(1 3)
drop countynum
*final vars
gen state="North Dakota"
merge m:1 state using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/primary-precincts/help-files/merge_on_statecodes.dta", nogen keep(1 3)
merge m:1 county_name state using "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/primary-precincts/help-files/county-fips-codes.dta", nogen keep(1 3)
tostring(county_fips), replace
gen jurisdiction_name = county_name
gen jurisdiction_fips = county_fips
gen stage = "GEN"
replace state = "NORTH DAKOTA"
gen date = "2020-11-03"
gen readme_check="FALSE"
gen year = 2020
gen magnitude=1
compress
order precinct office district candidate party_detailed party_simplified mode votes county_name county_fips jurisdiction_name jurisdiction_fips dataverse year stage state* special writein date read
sort office district county_name precinct
export delimited "/Users/cantstopkevin/Documents/HarvardDesktop/MEDSL/github/2020-precincts/precinct/ND/2020-nd-precinct-general.csv", replace