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
Add one overall note, text or caption to a combined coefplotHi I use coefplot to stack several graphs together. When I want to add a note, it adds to each sub g…
Calculate difference across rows by the value of a third variableHi experts, My data are like below. It consists of one variable price, and another variable group. …
Replace first row with variable namesHi All, I have data that resembles the following: Code: * Example generated by -dataex-. To ins…
Converting factor notation into dummies.Dear All, I want to conduct a two-stage least squares regression using the ivreg2 command. I have q…
if loops?Hello! I'm a PhD student and relatively new to Stata. I have medicaid data, and I am assessing for …
Subscribe to:
Post Comments (Atom)
0 Response to Reshape long to wide error
Post a Comment