Hello stateliest, Im having trouble trying to reshape my data set from long-wide. My code can be found below but if I was to try to do a final reshape wide it says that im having an error with my i () and j () variable because they must identify uniquely in the observation.
cd /Users/Desktop/county/
clear
save FL_2012.dta, replace emptyok
foreach county in ALA BAK BAY BRA BRE_1 BRE_2 BRE_3 BRO CAL CHA ///
CIT CLA CLL CLM DAD DES DIX DUV ESC FLA FRA GAD ///
GIL GLA GUL HAM HAR HEN HER HIG HIL HOL IND JAC ///
JEF LAF LAK LEE LEO LIB MAD MAN MON MRT NAS OKA ///
OKE OSC PAL PAS PIN PUT SAN SAR SEM STJ STL ///
SUM_2 SUM_1 SUW TAY UNI VOL WAK WAL WAS {
import delimited using "`county'.txt", clear
append using FL_2012.dta,
save FL_2012.dta, replace
}
rename v2 county
rename v7 precinct
rename v8 precinct_id
rename v12 race
rename v13 district
rename v14 race_code
rename v15 candidate
rename v16 partyid
rename v17 reg_num
rename v18 reten_num
rename v19 vote_total
drop v1 v3 v4 v5 v6 v9 v10 v11
*keeping party observations; dropping unaffiliated*
keep if partyid == "REP"| partyid == "DEM" | partyid == "LBT" ///
keep if race == "President of the United States" | race == "State Representative" |race == "State Senator" | race == "United States Senator" | race == "U.S. Representative"
*---------------------------------------------------------------------------
gen precinctid = partyid + "_" + precinct
encode precinctid, gen (precinctnum)
encode partyid, gen (partynum)
gen precinct_party = precinctnum + partynum
*gen ID = precinct_id*100 + partynum
egen ID = concat (precinct_id partynum)
encode race, gen (racenum)
egen total_votes = total (vote_total), by(candidate race)
sort total_votes
duplicates tag ID racenum, gen (tag)
duplicates drop
//---------------------------------------------------------------------//
*THIS IS WHERE MY ERROR OCCURS*
reshape wide candidate race, i(precinct_party) j(racenum)
//---------------------------------------------------------------------//
capture tostring precinct, replace
append using FL_2012.dta
save FL_2012.dta, replace
Related Posts with Reshape long to wide error
Help identifying namesDear all, I have a database with over forty thousand observations and I want to identify if the pers…
Renaming with renumbering Code: * Example generated by -dataex-. For more info, type help dataex clear input byte v1 str19 st…
Merge identifier and date within rangeHi all, I would like to merge two datasets by identifier and date within range dataset A: accountnum…
Problem wtih running and exporting first and second stage regressions with ivregress2 and getting the cluster standard errorHello, I am using ivregress2 to run and export the results of first and second stage regressions. I…
Problem wtih running and exporting first and second stage regressions with ivregress2 an getting the cluster standard errorHello, I am using ivregress2 to run and export the results of first and second stage regressions. I…
Subscribe to:
Post Comments (Atom)
0 Response to Reshape long to wide error
Post a Comment