/************************ Robocall Study insheet2006.do file created: March 9, 2012 file updated: March 11, 2012 This program takes the 2011 csv files (which have had candidate names replaced with party names) turns them into .dta files, merges them together into one big .dta file, and calculates some basic variables, such as voter turnout. It is somewhat more compactly written than insheet2011.do (there is only a single loop and no separate code for the first file, but it does the same thing. *************************/ /********************** * 1. Inputs and settings **********************/ clear clear matrix clear mata cd "\Users\Tom\Documents\economics\projects\robocalls\2006out\\" local path "D:\Users\Tom\Documents\economics\projects\robocalls\2006out\\" set mem 800m set more off /******** 2. Create filelist in other directory********/ ! dir *.csv /a-d /b > "\Users\Tom\Documents\economics\projects\robocalls\code\filelist2006.csv" /******** 3. Open each file, transform it into a .dta file and add ********/ file open myfile using "\Users\Tom\Documents\economics\projects\robocalls\code\filelist2006.csv", read file read myfile line local fcounter = 0 while r(eof)==0 { insheet using `line', comma names gen district_id = substr("`line'",26,5) qui describe local ncand = `r(k)' - 7 disp(`r(k)') gen electoraldistrictnumbernumrodeci = district_id == "fourtytwo" renvars electoraldistrictnumbernumrodeci electoraldistrictcirconscription pollingstationnamenomdubureaudes pollingstationnumbernumroduburea rejectedballotsbulletinsrejets \ id name pollplaceint pollnum invalidint local candnames pollplaceint-invalidint foreach n of varlist `candnames' { label variable `n' "`n'" } renvars pollplaceint-invalidint\pollplace v1-v`ncand' invalid drop if missing(v2) capture drop if v2=="NaN" foreach n of varlist v1-v`ncand' { destring(`n'), replace local x : variable label `n' rename `n' `x' } foreach v in "conservative" "liberal" "ndp" "greenparty" "bloc" { capture confirm variable `v' if !(_rc==0) { gen `v' = 0 } } order conservative liberal ndp greenparty bloc, first mat A = J(5, 1, 0) mat B = J(4, 1, 0) local counter = 1 foreach n of varlist conservative-bloc { qui sum `n' mat A[`counter',1] = `r(sum)' local counter = `counter' + 1 } mat B = A[2..5,1] /* this chooses only votes for non conservative parties * */ mata: st_matrix("A", sort(st_matrix("A"), 1)) gen edonevotes = A[5,1] gen edtwovotes = A[4,1] gen edthreevotes = A[3,1] gen edmargin = edonevotes - edtwovotes mat define C = B mata: st_matrix("C", sort(st_matrix("C"), 1)) if C[4,1] == B[1,1] { gen contendervotes = liberal gen contendername = "Liberal" } else if C[4,1] == B[2,1] { gen contendervotes = ndp gen contendername = "NDP" } else if C[4,1] == B[3,1] { gen contendervotes = greenparty gen contendername = "Green Party" } else if C[4,1] == B[4,1] { gen contendervotes = bloc gen contendername = "Bloc" } renvars totalvotestotaldesvotes electorslecteurs\totalvotes voters foreach n of varlist invalid-voters { replace voters = "" if voters == "NaN" destring(`n'), replace } egen edvoters = sum(voters) egen edvotes = sum(totalvotes) keep totalvotes invalid id name pollplace conservative liberal ndp greenparty bloc voters pollnum contendervotes edonevotes edtwovotes edthreevotes edvoters edvotes edmargin contendername tostring(pollnum), replace save `line'.dta, replace if `fcounter' >0 { append using master_data2006.dta } local fcounter = `fcounter' + 1 save master_data2006.dta, replace drop _all file read myfile line qui cd "\Users\Tom\Documents\economics\projects\robocalls\2006out\" } use master_data2006.dta, clear keep totalvotes invalid id name pollplace conservative liberal ndp greenparty bloc voters pollnum contendervotes edonevotes edtwovotes edthreevotes edvoters edvotes edmargin contendername gen year = 2006 save master_data2006.dta, replace file close myfile