Hi all,
I have an issue with a particular loop that I am trying to implement: my goal is to reshape a dataset (Manifesto Project) at the country-year-party level to have a dataset with just country-year and then 5 columns for each variables that shows variable x for party 1,2,3,4 and 5. This part is quite straightforward, my issue is with keeping labels when doing the reshape wide.
My data looks like this
I just kept France and Italy as an example
Now the following code ranks the top5 parties for each country and each year and reshapes wide and here comes the problem, apparently the loop just keep variable labels for the first country to which it is applied when it. goes to the second dataset, in this case Italy it does not label variables: Here's the full code:
Notice that I save a dataset in folder Partial before Reshaping because I need it later on for further cleaning
The specific code for applying labels is:
Can anyone spot my mistake? The code is not crashing or things like this but there are two issues: 1. It just applies labels to the first dataset of the loop 2. It doesn't work if parties are less than 5 so that the ranking procedure takes less than top 5 parties. Notice that if the procedure is done singularly for each country without the loop it works fine.
Thanks in advance for your help
Best
Guido
I have an issue with a particular loop that I am trying to implement: my goal is to reshape a dataset (Manifesto Project) at the country-year-party level to have a dataset with just country-year and then 5 columns for each variables that shows variable x for party 1,2,3,4 and 5. This part is quite straightforward, my issue is with keeping labels when doing the reshape wide.
My data looks like this
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str3 ISO float year int country_code str183 partyname double(pervote per101 per102) "FRA" 1973 31 "Socialist Party" 19.103 .4 .8 "FRA" 1956 31 "Union for the Defence of Traders and Artisans - Poujadists" 11.558 12.329 0 "FRA" 1956 31 "Rally for the French People - Gaullists" 3.92 9.903 0 "FRA" 2007 31 "National Front" 4.289 .571 .571 "FRA" 1986 31 "Socialist Party" 31.299 2.4 0 "FRA" 1946 31 "Republican Party of Liberty - Conservatives" 12.839 4.932 0 "FRA" 1978 31 "Socialist Party" 22.789 .8 0 "FRA" 2012 31 "Democratic Movement" 1.765 0 0 "FRA" 1993 31 "Union for French Democracy" 19.62 0 0 "FRA" 1993 31 "Rally for the Republic" 20.24 4.724 0 "FRA" 1997 31 "The Greens" 3.592 0 0 "FRA" 1997 31 "Ecology Generation" 2.674 0 0 "FRA" 1997 31 "Union for French Democracy" 14.689 1.639 0 "FRA" 1988 31 "Socialist Party" 36.557 1.7 0 "FRA" 1981 31 "Socialist Party" 36.57 2.6 0 "FRA" 1967 31 "Radical Socialist Party" 7.53 0 0 "FRA" 2002 31 "Union for French Democracy" 4.79 1.441 0 "FRA" 1958 31 "French Communist Party" 18.886 4.2 2.8 "FRA" 2007 31 "Union for a Popular Movement" 39.54 0 0 "FRA" 2012 31 "Left Front" 6.909 0 0 end label values country_code countries label def countries 31 "france", modify
I just kept France and Italy as an example
Now the following code ranks the top5 parties for each country and each year and reshapes wide and here comes the problem, apparently the loop just keep variable labels for the first country to which it is applied when it. goes to the second dataset, in this case Italy it does not label variables: Here's the full code:
Notice that I save a dataset in folder Partial before Reshaping because I need it later on for further cleaning
Code:
*********************************
* Some cleaning and merging ISO *
*********************************
duplicates drop country year party,force
rename country country_code
rename countryname country
replace country="Korea, Republic of" if country=="South Korea"
replace country="Russia Federation" if country=="Russia"
replace country="Bosnia and Herzegovina" if country=="Bosnia-Herzegovina"
drop if country=="Northern Ireland" | country=="German Democratic Republic"
*==================*
* Merging Isocodes *
*==================*
merge m:1 country using $dir/iso_def
drop if _merge ==2
drop _merge
order ISO year
********************************************
* Identify for each country-year the Top 5 parties *
********************************************
*================================================================*
* Dropping Belarus and Sri Lanka, no available obs for these two *
*================================================================*
drop if ISO=="BLR" | ISO=="LKA"
*---------------------------*
* Quick cleaning for Brasil *
*---------------------------*
replace pervote = presvote if pervote==.
g presidential="Presidential" if pervote==. & presvote!=.
*===========================*
* Looping over each country *
*===========================*
keep if ISO=="ITA" | ISO=="FRA"
levelsof ISO, local(countries)
foreach x of local countries {
preserve
*=========================*
* Keep country by country *
*=========================*
keep if ISO=="`x'"
gsort ISO year -pervote
drop if pervote==.
*===================================*
* Generating descending order count *
*===================================*
gen _1pervote=-pervote
bys year (_1pervote): g min= _n
*==============================*
* Dropping if outside of top 5 *
*==============================*
drop if min>5
drop _1pervote
*========================================*
* Reshaping to have country-year dataset *
*========================================*
gsort year -pervote
keep ISO year pervote min partyname
*--------------------------------------------------------------*
* This save is used in the next section to keep the party name *
*--------------------------------------------------------------*
save $Manifesto/Partial/`x', replace
drop partyname
gen min_string =""
replace min_string="1st_party" if min==1
replace min_string="2nd_Party" if min==2
replace min_string="3rd_Party" if min==3
replace min_string="4th_Party" if min==4
replace min_string="5th_Party" if min==5
drop min
**************************************************************************************
* The following Procedure keeps original labels and it adds the levels of min_string *
**************************************************************************************
local vlist "pervote"
local min "min_string"
*============================================================*
*Create label for each variable in vlist for each level of J *
*============================================================*
levelsof `min', local(J)
foreach var of varlist `vlist' {
foreach y of local J {
local newlist `newlist' `var'`y'
local lablist "`lablist' `"`:variable label `var'' (`y')"'"
}
}
*********************
* Reshaping Dataset *
*********************
reshape wide pervote, i(year) j(min_string) string
*=========================*
* LABEL the new variables *
*=========================*
foreach new of local newlist {
gettoken lab lablist : lablist
lab var `new' "`lab'"
}
*=================*
* Saving Datasets *
*=================*
save $Manifesto/To_merge/`x'_tomerge, replace
restore
}
The specific code for applying labels is:
Code:
**************************************************************************************
* The following Procedure keeps original labels and it adds the levels of min_string *
**************************************************************************************
local vlist "pervote"
local min "min_string"
*============================================================*
*Create label for each variable in vlist for each level of J *
*============================================================*
levelsof `min', local(J)
foreach var of varlist `vlist' {
foreach y of local J {
local newlist `newlist' `var'`y'
local lablist "`lablist' `"`:variable label `var'' (`y')"'"
}
}
*********************
* Reshaping Dataset *
*********************
reshape wide pervote, i(year) j(min_string) string
*=========================*
* LABEL the new variables *
*=========================*
foreach new of local newlist {
gettoken lab lablist : lablist
lab var `new' "`lab'"
}
Thanks in advance for your help
Best
Guido
