*! Merge EU-Silc longitudinal datasets into one large panel
*! version Beta 1.0  18oct17           by Marwin Borst (marwin.borst@live.it)
*! version Beta 1.1  01-07-2022 Update by Heike Wirth  (heike.wirth@gesis.org)

program eusilcpanel_2020
	version 13
	display "EUsilcPanel Beta 1.1 for UDB ver. 2022"
	display " "
	display "SYSTEM REQUIREMENTS: 8gb RAM, 30gb free discspace, Win 10"
	display "                     Stata/MP 13, hours of patience"
	display " "
	display " "


	/**************************************************************/
	/*  eusilcpanel_2020    UDB - release 2022_03                 */
	/*	EUsilcPanel Beta release for UDB ver. 2022                */
	/*	EUsilcPanel transforms the EU-silc longitudinal           */
	/*  UDB files (2022 release) into 4 datasets (D,H,R,P-Files)  */
	/*  containing observations from 2003 to 2020                 */
	/*  UPDATE of eusilcpanel                                     */
	/*                                                            */
	/*	Marwin Borst        marwin.borst@live.it                  */
    /*  Update:             heike.wirth@gesis.org                 */
	/*                                                            */
    /*                                                            */
	/*  Please acknowledge this work by citing both the           */
    /*  the paper and the script  .                               */
    /*  Borst, Marwin/Wirth, Heike (2022):                        */
    /*	EU-SILC Tools: eusilcpanel_2022                           */
    /*  First computational steps towards a cumulative            */
    /*  sample based on the EU-SILC longitudinal datasets         */
    /*  GESIS Papers 2022/10 (update of GESIS Papers 2018/11)     */
    /*  https://www.gesis.org/gml/european-microdata/eu-silc/     */
    /*  => eusilcpanel                                            */
    /*                                                            */
	/*  eusilcpanel_2020 is based on EU-SILC Stata systemfiles,   */
    /*	The transformation of the CSV files into Stata files      */
	/*  was done with the latest Stata scripts (setups) provided  */
	/*  by GESIS, German Microdata Lab                            */
	/*  https://www.gesis.org/en/missy/materials/EU-SILC/setups   */
	/*  https://www.gesis.org/ssoar/home                          */
    /*                                                            */
    /*  contact: eu-microdata@gesis.org                           */
	/**************************************************************/

	display "Prepare the Stata systemfiles and check if folder names"
    display "correspond to naming convention (see help eusilcpanel)."
	display ""
	display "Right-click the folder 'EU-SILC' go to 'Properties' and copy"
	display "its location. Then paste it in the command field below as is, without adding"
	display "any quotation marks or slashes(for example: C:\Users\Paul\Desktop)" _request(datapath)


	/* D-FILES */

	/* 2020 */
	/*open the 2020 Household register to get list of rotation groups with maxobs and starting point for masterfile*/
	clear
	set more off
	local filen : dir "${datapath}\EU-SILC\16 L-2020\"  files "udb_l20d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\16 L-2020\"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'

	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	replace country = "EL" if country == "GR"
	sort country hid year
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2020 = total(nvals)
	drop nvals
	/* mark selected rotation group (this is the most recent release, so all rotationgroups are selected)*/
	gen slctd_rtgrp = rotation_group
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	gen drpout_year = maxyear
	replace drpout_year = drpout_year + 4 - years_cov
		/*check for rotation groups that ended earlier than the latest year*/
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	drop rotation_groupstr maxyear_grp minyear years_cov maxyear_grp maxyear
	destring drpout_year, replace
	/*for later checks*/
	gen drpout_year2020 = drpout_year
	gen slctd_urtgrp2020 = urtgrp
	gen slctd_uhid2020 = uhid
	/*this is the masterfile to build the dataset on */
	gen merge2020 = 1
	save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
	/*this is the the 2020D file for later control.*/
	drop merge2020
	save "${datapath}\EU-SILC\16 L-2020\2020D.dta", replace

/* 2019 */

	/*Open the 2019 Household register to get data from rotational groups inactive in 2020 and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\15 L-2019\"  files "udb_l19d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\15 L-2019/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'

	use `"`fileln'"', clear

	local new_var=lower("`var'")

	 foreach var of varlist _all {
	local new_var = lower("`var'")
	cap rename `var' `new_var'
	}

	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2019 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\16 L-2020\2020D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*update urtgrp and uhid*/
		drop maxyear rotation_groupstr
		bysort country rotation_group: egen maxyear = max(drpout_year)
		replace drpout_year = maxyear
		tostring drpout_year, replace
		tostring rotation_group, generate(rotation_groupstr)
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		keep if _merge == 1
		drop rotation_groupstr
	drop _merge maxyear_grp minyear years_cov maxyear_grp maxyear
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2019 = drpout_year
	gen slctd_urtgrp2019 = urtgrp
	gen slctd_uhid2019 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2020)
			replace test3 = 0 if test3 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3
		/*this is the updated masterfile */
		gen merge2019 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2019D file for later control*/
		keep if merge2019 == 1
		drop merge2019
		save "${datapath}\EU-SILC\15 L-2019\2019D.dta", replace


/* 2018 */

	/*open the 2018 Household register to get data from rotational groups inactive in 2019 and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\14 L-2018\"  files "udb_l18d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\14 L-2018/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'

	use `"`fileln'"', clear

	local new_var=lower("`var'")

	 foreach var of varlist _all {
	local new_var = lower("`var'")
	cap rename `var' `new_var'
	}

	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2018 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2019 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\15 L-2019\2019D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2019)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2020 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\16 L-2020\2020D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2018 = drpout_year
	gen slctd_urtgrp2018 = urtgrp
	gen slctd_uhid2018 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2019)
			bysort country : egen test4 = total(nrtgrp2020)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4
		/*this is the updated masterfile */
		gen merge2018 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2018D file for later control*/
		keep if merge2018 == 1
		drop merge2018
		save "${datapath}\EU-SILC\14 L-2018\2018D.dta", replace



/* 2017 */

	/*open the 2017 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\13 L-2017\"  files "udb_l17d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\13 L-2017/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'


	use `"`fileln'"', clear

	local new_var=lower("`var'")

	foreach var of varlist _all {
		local new_var = lower("`var'")
		cap rename `var' `new_var'
	}

	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2017 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2018 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\14 L-2018\2018D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2018)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2019 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\15 L-2019\2019D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2020 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\16 L-2020\2020D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2017 = drpout_year
	gen slctd_urtgrp2017 = urtgrp
	gen slctd_uhid2017 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2018)
			bysort country : egen test4 = total(nrtgrp2019)
			bysort country : egen test5 = total(nrtgrp2020)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2017 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2017D file for later control*/
		keep if merge2017 == 1
		drop merge2017
		save "${datapath}\EU-SILC\13 L-2017\2017D.dta", replace




/* 2016 */


	/*open the 2016 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\12 L-2016\"  files "udb_l16d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\12 L-2016/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'


	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2016 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2017 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\13 L-2017\2017D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2017)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2018 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\14 L-2018\2018D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2019 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\15 L-2019\2019D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2016 = drpout_year
	gen slctd_urtgrp2016 = urtgrp
	gen slctd_uhid2016 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2017)
			bysort country : egen test4 = total(nrtgrp2018)
			bysort country : egen test5 = total(nrtgrp2019)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2016 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2016D file for later control*/
		keep if merge2016 == 1
		drop merge2016
		save "${datapath}\EU-SILC\12 L-2016\2016D.dta", replace



	/* 2015 */

	/*open the 2015 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\11 L-2015\"  files "udb_l15d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\11 L-2015/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

	foreach var of varlist _all {
		local new_var = lower("`var'")
		cap rename `var' `new_var'
	}

	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2015 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2016 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\12 L-2016\2016D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2016)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2017 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\13 L-2017\2017D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2018 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\14 L-2018\2018D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2015 = drpout_year
	gen slctd_urtgrp2015 = urtgrp
	gen slctd_uhid2015 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2016)
			bysort country : egen test4 = total(nrtgrp2017)
			bysort country : egen test5 = total(nrtgrp2018)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2015 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2015D file for later control*/
		keep if merge2015 == 1
		drop merge2015
		save "${datapath}\EU-SILC\11 L-2015\2015D.dta", replace

/* 2014 */

	/*open the 2014 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\10 L-2014\"  files "udb_l14d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\10 L-2014/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

	foreach var of varlist _all {
		local new_var = lower("`var'")
		cap rename `var' `new_var'
	}

	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2014 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2016 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\12 L-2016\2016D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2016)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2017 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\13 L-2017\2017D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2015 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\11 L-2015\2015D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2014 = drpout_year
	gen slctd_urtgrp2014 = urtgrp
	gen slctd_uhid2014 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2016)
			bysort country : egen test4 = total(nrtgrp2017)
			bysort country : egen test5 = total(nrtgrp2015)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2014 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2014D file for later control*/
		keep if merge2014 == 1
		drop merge2014
		save "${datapath}\EU-SILC\10 L-2014\2014D.dta", replace

/* 2013 */

	/*open the 2013 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\9 L-2013\"  files "udb_l13d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\9 L-2013/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

	foreach var of varlist _all {
		local new_var = lower("`var'")
		cap rename `var' `new_var'
	}

	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2013 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2016 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\12 L-2016\2016D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2016)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2014 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\10 L-2014\2014D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2015 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\11 L-2015\2015D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2013 = drpout_year
	gen slctd_urtgrp2013 = urtgrp
	gen slctd_uhid2013 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2016)
			bysort country : egen test4 = total(nrtgrp2014)
			bysort country : egen test5 = total(nrtgrp2015)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2013 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2013D file for later control*/
		keep if merge2013 == 1
		drop merge2013
		save "${datapath}\EU-SILC\9 L-2013\2013D.dta", replace

/* 2012 */

	/*open the 2012 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\8 L-2012\"  files "udb_l12d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\8 L-2012/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2012 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2013 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\9 L-2013\2013D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2013)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2014 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\10 L-2014\2014D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2015 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\11 L-2015\2015D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2012 = drpout_year
	gen slctd_urtgrp2012 = urtgrp
	gen slctd_uhid2012 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2013)
			bysort country : egen test4 = total(nrtgrp2014)
			bysort country : egen test5 = total(nrtgrp2015)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2012 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2012D file for later control*/
		keep if merge2012 == 1
		drop merge2012
		save "${datapath}\EU-SILC\8 L-2012\2012D.dta", replace

	/* 2011 */

	/*open the 2011 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\7 L-2011\"  files "udb_l11d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\7 L-2011/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2011 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2012 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\8 L-2012\2012D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2012)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2013 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\9 L-2013\2013D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2014 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\10 L-2014\2014D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2011 = drpout_year
	gen slctd_urtgrp2011 = urtgrp
	gen slctd_uhid2011 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2012)
			bysort country : egen test4 = total(nrtgrp2013)
			bysort country : egen test5 = total(nrtgrp2014)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2011 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2011D file for later control*/
		keep if merge2011 == 1
		drop merge2011
		save "${datapath}\EU-SILC\7 L-2011\2011D.dta", replace

/* 2010 */

	/*open the 2010 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\6 L-2010\"  files "udb_l10d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\6 L-2010/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2010 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2011 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\7 L-2011\2011D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2011)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2012 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\8 L-2012\2012D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2013 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\9 L-2013\2013D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2010 = drpout_year
	gen slctd_urtgrp2010 = urtgrp
	gen slctd_uhid2010 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2011)
			bysort country : egen test4 = total(nrtgrp2012)
			bysort country : egen test5 = total(nrtgrp2013)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2010 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2010D file for later control*/
		keep if merge2010 == 1
		drop merge2010
		save "${datapath}\EU-SILC\6 L-2010\2010D.dta", replace

/* 2009 */

	/*open the 2009 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\5 L-2009\"  files "udb_l09d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\5 L-2009/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2009 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2010 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\6 L-2010\2010D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2010)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2011 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\7 L-2011\2011D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2012 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\8 L-2012\2012D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2009 = drpout_year
	gen slctd_urtgrp2009 = urtgrp
	gen slctd_uhid2009 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2010)
			bysort country : egen test4 = total(nrtgrp2011)
			bysort country : egen test5 = total(nrtgrp2012)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2009 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2009D file for later control*/
		keep if merge2009 == 1
		drop merge2009
		save "${datapath}\EU-SILC\5 L-2009\2009D.dta", replace

/* 2008 */

	/*open the 2008 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
	clear
	local filen : dir "${datapath}\EU-SILC\4 L-2008\"  files "udb_l08d_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\4 L-2008/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring db030, replace
	gen year=db010
	gen country=db020
	gen hid=db030
	gen rotation_group=db075
	sort country hid year
	replace country = "EL" if country == "GR"
	/* count the number of rotational groups for each country in this release*/
	bysort country rotation_group: gen nvals = _n == 1
	bysort country : egen nrtgrp2008 = total(nvals)
	drop nvals
	/* get the rotation group(s) that cover most years*/
	bysort country rotation_group : egen maxyear = max(year)
	bysort country rotation_group : egen minyear = min(year)
	gen years_cov = maxyear - minyear + 1
	bysort country : egen maxgrp = max(years_cov)
	gen slctd_rtgrp=0
	bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
	gen lgstgrp = 0
	bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
	/* build household and rotationgroup IDs that are unique across releases */
	tostring rotation_group, generate(rotation_groupstr)
	gen drpout_year = 0
	replace drpout_year = maxyear + 4 - years_cov
		/*check for rotation groups that dropped out before planned */
		bysort country rotation_group : egen maxyear_grp = max(year)
		replace drpout_year = maxyear_grp if maxyear_grp < maxyear
	tostring drpout_year, replace
	gen uhid = country + rotation_groupstr + drpout_year + hid
	gen urtgrp = country + rotation_groupstr + drpout_year
	destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2009 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\5 L-2009\2009D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2009)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2010 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\6 L-2010\2010D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2011 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\7 L-2011\2011D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2008 = drpout_year
	gen slctd_urtgrp2008 = urtgrp
	gen slctd_uhid2008 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2009)
			bysort country : egen test4 = total(nrtgrp2010)
			bysort country : egen test5 = total(nrtgrp2011)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2008 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2008D file for later control*/
		keep if merge2008 == 1
		drop merge2008
		save "${datapath}\EU-SILC\4 L-2008\2008D.dta", replace

/* 2007 */
		/*open the 2007 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
		clear
		local filen : dir "${datapath}\EU-SILC\3 L-2007\"  files "udb_l07d_ver*"
		display `filen'
		local filel "${datapath}\EU-SILC\3 L-2007/"
		display `"`filel'"'
		local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


		/*Block Stata from displaying IDs in exponential format*/
		tostring db030, replace
		gen year=db010
		gen country=db020
		gen hid=db030
		gen rotation_group=db075
		sort country hid year
		replace country = "EL" if country == "GR"
		/* count the number of rotational groups for each country in this release*/
		bysort country rotation_group: gen nvals = _n == 1
		bysort country : egen nrtgrp2007 = total(nvals)
		drop nvals
		/* get the rotation group(s) that cover most years*/
		bysort country rotation_group : egen maxyear = max(year)
		bysort country rotation_group : egen minyear = min(year)
		gen years_cov = maxyear - minyear + 1
		bysort country : egen maxgrp = max(years_cov)
		gen slctd_rtgrp=0
		bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
		gen lgstgrp = 0
		bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
		/* build household and rotationgroup IDs that are unique across releases */
		tostring rotation_group, generate(rotation_groupstr)
		gen drpout_year = 0
		replace drpout_year = maxyear + 4 - years_cov
			/*check for rotation groups that dropped out before planned */
			bysort country rotation_group : egen maxyear_grp = max(year)
			replace drpout_year = maxyear_grp if maxyear_grp < maxyear
		tostring drpout_year, replace
		gen uhid = country + rotation_groupstr + drpout_year + hid
		gen urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2008 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\4 L-2008\2008D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2008)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2009 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\5 L-2009\2009D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2010 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\6 L-2010\2010D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2007 = drpout_year
	gen slctd_urtgrp2007 = urtgrp
	gen slctd_uhid2007 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2008)
			bysort country : egen test4 = total(nrtgrp2009)
			bysort country : egen test5 = total(nrtgrp2010)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2007 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2007D file for later control*/
		keep if merge2007 == 1
		drop merge2007
		save "${datapath}\EU-SILC\3 L-2007\2007D.dta", replace

/* 2006 */

		/*open the 2006 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
		clear
		local filen : dir "${datapath}\EU-SILC\2 L-2006\"  files "udb_l06d_ver*"
		display `filen'
		local filel "${datapath}\EU-SILC\2 L-2006/"
		display `"`filel'"'
		local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


		/*Block Stata from displaying IDs in exponential format*/
		tostring db030, replace
		gen year=db010
		gen country=db020
		gen hid=db030
		gen rotation_group=db075
		sort country hid year
		replace country = "EL" if country == "GR"
		/* count the number of rotational groups for each country in this release*/
		bysort country rotation_group: gen nvals = _n == 1
		bysort country : egen nrtgrp2006 = total(nvals)
		drop nvals
		/* get the rotation group(s) that cover most years*/
		bysort country rotation_group : egen maxyear = max(year)
		bysort country rotation_group : egen minyear = min(year)
		gen years_cov = maxyear - minyear + 1
		bysort country : egen maxgrp = max(years_cov)
		gen slctd_rtgrp=0
		bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
		gen lgstgrp = 0
		bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
		/* build household and rotationgroup IDs that are unique across releases */
		tostring rotation_group, generate(rotation_groupstr)
		gen drpout_year = 0
		replace drpout_year = maxyear + 4 - years_cov
			/*check for rotation groups that dropped out before planned */
			bysort country rotation_group : egen maxyear_grp = max(year)
			replace drpout_year = maxyear_grp if maxyear_grp < maxyear
		tostring drpout_year, replace
		gen uhid = country + rotation_groupstr + drpout_year + hid
		gen urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2007 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\3 L-2007\2007D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2007)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2008 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\4 L-2008\2008D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2009 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\5 L-2009\2009D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2006 = drpout_year
	gen slctd_urtgrp2006 = urtgrp
	gen slctd_uhid2006 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2007)
			bysort country : egen test4 = total(nrtgrp2008)
			bysort country : egen test5 = total(nrtgrp2009)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2006 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2006D file for later control*/
		keep if merge2006 == 1
		drop merge2006
		save "${datapath}\EU-SILC\2 L-2006\2006D.dta", replace

/* 2005 */

		/*open the 2005 Household register to get data from rotational groups inactive in more recent releases and their uhid*/
		clear
		local filen : dir "${datapath}\EU-SILC\1 L-2005\"  files "udb_l05d_ver*"
		display `filen'
		local filel "${datapath}\EU-SILC\1 L-2005/"
		display `"`filel'"'
		local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


		/*Block Stata from displaying IDs in exponential format*/
		tostring db030, replace
		gen year=db010
		gen country=db020
		gen hid=db030
		gen rotation_group=db075
		sort country hid year
		replace country = "EL" if country == "GR"
		/* count the number of rotational groups for each country in this release*/
		bysort country rotation_group: gen nvals = _n == 1
		bysort country : egen nrtgrp2005 = total(nvals)
		drop nvals
		/* get the rotation group(s) that cover most years*/
		bysort country rotation_group : egen maxyear = max(year)
		bysort country rotation_group : egen minyear = min(year)
		gen years_cov = maxyear - minyear + 1
		bysort country : egen maxgrp = max(years_cov)
		gen slctd_rtgrp=0
		bysort country rotation_group: replace slctd_rtgrp = rotation_group if years_cov == maxgrp
		gen lgstgrp = 0
		bysort country rotation_group: replace lgstgrp = 1 if years_cov == maxgrp
		/* build household and rotationgroup IDs that are unique across releases */
		tostring rotation_group, generate(rotation_groupstr)
		gen drpout_year = 0
		replace drpout_year = maxyear + 4 - years_cov
			/*check for rotation groups that dropped out before planned */
			bysort country rotation_group : egen maxyear_grp = max(year)
			replace drpout_year = maxyear_grp if maxyear_grp < maxyear
		tostring drpout_year, replace
		gen uhid = country + rotation_groupstr + drpout_year + hid
		gen urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		/*check if any selected groups have been selected in the more recent 2006 release ("prolonged rotation groups")*/
		sort year country rotation_group hid
		merge m:m year country rotation_group using "${datapath}\EU-SILC\2 L-2006\2006D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		/*tag rotation groups that have already been selected in the more recent release in some years (overlapping)*/
		gen atag = 0
		replace atag = 1 if _merge == 3
		bysort country rotation_group : egen btag = max(atag)
		bysort country rotation_group : egen ndrpoy = max(drpout_year2006)
		replace drpout_year = ndrpoy if btag == 1
		drop atag btag ndrpoy
		/*update urtgrp and uhid so that "prolonged rotation groups" maintain their urtgrp across different releases*/
		tostring drpout_year, replace
		replace uhid = country + rotation_groupstr + drpout_year + hid
		replace urtgrp = country + rotation_groupstr + drpout_year
		destring drpout_year, replace
		drop rotation_groupstr maxyear_grp minyear years_cov maxgrp maxyear_grp maxyear
		/*drop overlapping rotation group years and data from the more recent release*/
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2007 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\3 L-2007\2007D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
		/*check if any selected groups have been selected in the more recent 2008 release ("prolonged rotation groups")*/
		merge m:m year country urtgrp using "${datapath}\EU-SILC\4 L-2008\2008D.dta"
		replace slctd_rtgrp = rotation_group if _merge == 1
		keep if _merge==1
		drop _merge
	destring drpout_year, replace
	drop if slctd_rtgrp == 0
	/*for later checks*/
	gen drpout_year2005 = drpout_year
	gen slctd_urtgrp2005 = urtgrp
	gen slctd_uhid2005 = uhid
	/*merge to masterfile*/
	merge 1:1 year uhid using "${datapath}\EU-SILC\16 L-2020\masterD.dta"
		/*check for duplicates caused by same rotational groups in terms of same hid, different rotation_group*/
		duplicates report year country hid
		duplicates tag year country hid, generate(test2)
		tab country year if test2 != 0
		tab urtgrp year  if test2 != 0
			/*check if the problem regards a large portion of the rotational group, if the portion is small ignore*/
			gen tag1 = 0
			replace tag1 = 1 if test2 != 0
			bysort year country urtgrp : egen ndups = total(tag1)
			gen tag2 = 1
			bysort year country urtgrp : egen tot = total(tag2)
			gen rdups = ndups / tot
			gen tag3 = 0
			replace tag3 = 1 if rdups > 0.5
			drop tag1 tag2 ndups tot rdups
			/* extend selection to whole rotational group*/
			bysort year country urtgrp : egen taga = max(tag3)
			drop tag3
			/*check if the rotational group with duplicates is the one covering most years in current release. If yes ignore*/
			tab urtgrp year if taga == 1  & lgstgrp == 0 & _merge == 1
			drop if taga == 1 & lgstgrp == 0 & _merge == 1
			drop  test2 taga
			/*check for unbalances*/
			bysort country : egen test3 = total(nrtgrp2006)
			bysort country : egen test4 = total(nrtgrp2007)
			bysort country : egen test5 = total(nrtgrp2008)
			replace test3 = 0 if test3 == .
			replace test4 = 0 if test4 == .
			replace test5 = 0 if test5 == .
			tab urtgrp year if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop if ( test3 != 0 & lgstgrp == 0 & _merge == 1 ) & ( test4 != 0 & lgstgrp == 0 & _merge == 1 ) &( test5 != 0 & lgstgrp == 0 & _merge == 1 )
			drop test3 test4 test5
		/*this is the updated masterfile */
		gen merge2005 = _merge
		drop _merge
		save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace
		/*this is 2005D file for later control*/
		keep if merge2005 == 1
		drop merge2005
		save "${datapath}\EU-SILC\1 L-2005\2005D.dta", replace
	/*clean up and generate yrelease*/
	use "${datapath}\EU-SILC\16 L-2020\masterD.dta", clear
	gen yrelease = 0
	replace yrelease = 2020 if merge2020 == 1
	replace yrelease = 2019 if merge2019 == 1
	replace yrelease = 2018 if merge2018 == 1
	replace yrelease = 2017 if merge2017 == 1
	replace yrelease = 2016 if merge2016 == 1
	replace yrelease = 2015 if merge2015 == 1
	replace yrelease = 2014 if merge2014 == 1
	replace yrelease = 2013 if merge2013 == 1
	replace yrelease = 2012 if merge2012 == 1
	replace yrelease = 2011 if merge2011 == 1
	replace yrelease = 2010 if merge2010 == 1
	replace yrelease = 2009 if merge2009 == 1
	replace yrelease = 2008 if merge2008 == 1
	replace yrelease = 2007 if merge2007 == 1
	replace yrelease = 2006 if merge2006 == 1
	replace yrelease = 2005 if merge2005 == 1

	drop  slctd_rtgrp lgstgrp drpout_year maxgrp ///
	      nrtgrp*  drpout_year* slctd_urtgrp*  slctd_uhi*  merge*


	save "${datapath}\EU-SILC\16 L-2020\masterD.dta", replace


	/***/
	/* create one H file containing data from all releases */
	/***/

	/* 2020 */
	clear
	local filen : dir "${datapath}\EU-SILC\16 L-2020\"  files "udb_l20h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\16 L-2020/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	*Block Stata from displaying IDs in exponential format*
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	gen country = hb020
	sort year country hid
	*checking for duplicates or errors in hid*
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\16 L-2020\2020D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\16 L-2020\masterH.dta", replace

	/* 2019 */

	clear
	local filen : dir "${datapath}\EU-SILC\15 L-2019\"  files "udb_l19h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\15 L-2019/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	*Block Stata from displaying IDs in exponential format*
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	gen country = hb020
	sort year country hid
	*checking for duplicates or errors in hid*
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\15 L-2019\2019D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\15 L-2019\2019H.dta", replace

	/* 2018 */

	clear
	local filen : dir "${datapath}\EU-SILC\14 L-2018\"  files "udb_l18h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\14 L-2018/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	*Block Stata from displaying IDs in exponential format*
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	gen country = hb020
	sort year country hid
	*checking for duplicates or errors in hid*
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\14 L-2018\2018D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\14 L-2018\2018H.dta", replace

	/* 2017 */

	clear
	local filen : dir "${datapath}\EU-SILC\13 L-2017\"  files "udb_l17h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\13 L-2017/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	*Block Stata from displaying IDs in exponential format*
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	gen country = hb020
	sort year country hid
	*checking for duplicates or errors in hid*
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\13 L-2017\2017D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\13 L-2017\2017H.dta", replace

	/* 2016 */

	clear
	local filen : dir "${datapath}\EU-SILC\12 L-2016\"  files "udb_l16h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\12 L-2016/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	*Block Stata from displaying IDs in exponential format*
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	gen country = hb020
	sort year country hid
	*checking for duplicates or errors in hid*
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\12 L-2016\2016D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\12 L-2016\2016H.dta", replace



	/* 2015 */
	clear
	local filen : dir "${datapath}\EU-SILC\11 L-2015\"  files "udb_l15h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\11 L-2015/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	*Block Stata from displaying IDs in exponential format*
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	gen country = hb020
	sort year country hid
	*checking for duplicates or errors in hid*
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\11 L-2015\2015D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\11 L-2015\2015H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\10 L-2014\" files "udb_l14h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\10 L-2014/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\10 L-2014\2014D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\10 L-2014\2014H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\9 L-2013\" files "udb_l13h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\9 L-2013/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\9 L-2013\2013D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\9 L-2013\2013H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\8 L-2012\" files "udb_l12h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\8 L-2012/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\8 L-2012\2012D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\8 L-2012\2012H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\7 L-2011\" files "udb_l11h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\7 L-2011/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\7 L-2011\2011D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\7 L-2011\2011H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\6 L-2010\" files "udb_l10h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\6 L-2010/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\6 L-2010\2010D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\6 L-2010\2010H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\5 L-2009\" files "udb_l09h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\5 L-2009/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\5 L-2009\2009D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\5 L-2009\2009H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\4 L-2008\" files "udb_l08h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\4 L-2008/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\4 L-2008\2008D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\4 L-2008\2008H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\3 L-2007\" files "udb_l07h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\3 L-2007/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\3 L-2007\2007D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\3 L-2007\2007H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\2 L-2006\" files "udb_l06h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\2 L-2006/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\2 L-2006\2006D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\2 L-2006\2006H.dta", replace

	clear
	local filen : dir "${datapath}\EU-SILC\1 L-2005\" files "udb_l05h_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\1 L-2005/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	/*Block Stata from displaying IDs in exponential format*/
	tostring hb030, replace
	gen year = hb010
	gen hid = hb030
	tostring hid, replace
	gen country = hb020
	sort year country hid
	/*checking for duplicates or errors in hid*/
	duplicates report country year hid
	*select observations/households by merging with selected rotational groups from the D file *
	merge 1:1 year country hid using "${datapath}\EU-SILC\1 L-2005\2005D.dta"
	keep if _merge==3
	drop _merge
	destring hb100 , replace force
	save "${datapath}\EU-SILC\1 L-2005\2005H.dta", replace

	/*merge masterH with the 20XXH files from previous releases.*/
	use "${datapath}\EU-SILC\16 L-2020\masterH.dta", clear
	merge 1:1 year uhid using "${datapath}\EU-SILC\15 L-2019\2019H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\14 L-2018\2018H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\13 L-2017\2017H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\12 L-2016\2016H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\11 L-2015\2015H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\10 L-2014\2014H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\9 L-2013\2013H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\8 L-2012\2012H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\7 L-2011\2011H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\6 L-2010\2010H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\5 L-2009\2009H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\4 L-2008\2008H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\3 L-2007\2007H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\2 L-2006\2006H.dta"
	drop _merge
	merge 1:1 year uhid using "${datapath}\EU-SILC\1 L-2005\2005H.dta"
	drop _merge

	/*drop superflous variables*/
	drop db010 db020 db030 db040 db040_f db060 db060_f db062 db062_f db070 db070_f db075 db075_f db095 db095_f db100 db100_f db110 db110_f ///
	nrtgrp* slctd_rtgrp drpout_year drpout_year* slctd_uhid* slctd_urtgrp* ///
	maxgrp lgstgrp db090 db090_f merge*

	save "${datapath}\EU-SILC\16 L-2020\masterH.dta", replace

	/***/
	/*build masterfile for personal register files (R files)*/
	/***/

		/* 2020 */

	clear
	local filen : dir "${datapath}\EU-SILC\16 L-2020\"  files "udb_l20r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\16 L-2020/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\16 L-2020\2020D.dta"
	keep if _merge == 3
	drop _merge
	/*generate personal IDs that are unique across all releases by taking the last 2 digits of pid (personal number) and merging it to uhid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in pid alone but not in uhid and upid */
	gen suhid = substr(uhid,1,7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\16 L-2020\2020R", replace


	/* 2019 */

	clear
	local filen : dir "${datapath}\EU-SILC\15 L-2019\"  files "udb_l19r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\15 L-2019/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\15 L-2019\2019D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the last 2 digits of pid (personal number) and merging it to uhid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in pid alone but not in uhid and upid */
	gen suhid = substr(uhid,1,7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\15 L-2019\2019R", replace


	/* 2018 */

	clear
	local filen : dir "${datapath}\EU-SILC\14 L-2018\"  files "udb_l18r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\14 L-2018/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\14 L-2018\2018D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the last 2 digits of pid (personal number) and merging it to uhid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in pid alone but not in uhid and upid */
	gen suhid = substr(uhid,1,7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\14 L-2018\2018R", replace


	/* 2017 */

	clear
	local filen : dir "${datapath}\EU-SILC\13 L-2017\"  files "udb_l17r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\13 L-2017/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\13 L-2017\2017D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the last 2 digits of pid (personal number) and merging it to uhid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in pid alone but not in uhid and upid */
	gen suhid = substr(uhid,1,7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\13 L-2017\2017R", replace


	/* 2016 */

	clear
	local filen : dir "${datapath}\EU-SILC\12 L-2016\"  files "udb_l16r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\12 L-2016/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\12 L-2016\2016D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the last 2 digits of pid (personal number) and merging it to uhid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in pid alone but not in uhid and upid */
	gen suhid = substr(uhid,1,7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\12 L-2016\2016R", replace


	/* 2015 */

	clear
	local filen : dir "${datapath}\EU-SILC\11 L-2015\"  files "udb_l15r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\11 L-2015/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\11 L-2015\2015D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the last 2 digits of pid (personal number) and merging it to uhid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in pid alone but not in uhid and upid */
	gen suhid = substr(uhid,1,7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\11 L-2015\2015R", replace

	/*merge indivdual registers with 20XXslcd files to select the individuals contained in the rotational groups from previous releases we are interested in */
	/*merging 2014 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\10 L-2014\"  files "udb_l14r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\10 L-2014/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\10 L-2014\2014D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\10 L-2014\2014R", replace

	/*merging 2013 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\9 L-2013\"  files "udb_l13r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\9 L-2013/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }


	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\9 L-2013\2013D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\9 L-2013\2013R", replace

	/*merging 2012 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\8 L-2012\"  files "udb_l12r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\8 L-2012/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\8 L-2012\2012D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\8 L-2012\2012R", replace

	/*merging 2011 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\7 L-2011\"  files "udb_l11r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\7 L-2011/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\7 L-2011\2011D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\7 L-2011\2011R", replace

	/*merging 2010 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\6 L-2010\"  files "udb_l10r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\6 L-2010/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\6 L-2010\2010D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\6 L-2010\2010R", replace

	/*merging 2009 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\5 L-2009\"  files "udb_l09r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\5 L-2009/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\5 L-2009\2009D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\5 L-2009\2009R", replace

	/*merging 2008 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\4 L-2008\"  files "udb_l08r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\4 L-2008/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\4 L-2008\2008D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\4 L-2008\2008R", replace

	/*merging 2008 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\3 L-2007\"  files "udb_l07r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\3 L-2007/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\3 L-2007\2007D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\3 L-2007\2007R", replace

	/*merging 2006 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\2 L-2006\"  files "udb_l06r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\2 L-2006/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
 	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\2 L-2006\2006D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\2 L-2006\2006R", replace

	/*merging 2005 data*/
	clear
	local filen : dir "${datapath}\EU-SILC\1 L-2005\"  files "udb_l05r_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\1 L-2005/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring rb030, replace format("%15.0f")
	tostring rb040, replace
	gen year = rb010
	gen hid = rb040
	gen pid = rb030
	gen country = rb020
	sort year country hid
	/*checking for duplicates or errors in id*/
	duplicates report country year hid pid
	duplicates report country year pid
	/*merge with IDs from house hold register (D file) and drop households that are only in the D file, but not in the R file */
	merge m:1 year country hid using "${datapath}\EU-SILC\1 L-2005\2005D.dta"
	keep if _merge == 3
	drop _merge
	/*genereate personal IDs that are unique across all releases by taking the first 7 numbers of uhid and adding them to pid*/
	/*keep in mind that there are some individuals present in more than one familiy, so there are duplicates in upid alone but not in uhid and upid */
	gen suhid = substr(uhid, 1, 7)
	gen upid = suhid + pid
	drop suhid
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\1 L-2005\2005R", replace

	/*merge data from all releases into one masterfile.
	!! this process is memory intensive. */

	use "${datapath}\EU-SILC\16 L-2020\2020R.dta", clear

	merge 1:1 year upid uhid using "${datapath}\EU-SILC\15 L-2019\2019R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\14 L-2018\2018R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\13 L-2017\2017R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\12 L-2016\2016R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\11 L-2015\2015R.dta"
	drop _merge
	save "${datapath}\EU-SILC\16 L-2020\masterR.dta", replace
	clear
	use "${datapath}\EU-SILC\\16 L-2020\masterR.dta", clear

	merge 1:1 year upid uhid using "${datapath}\EU-SILC\10 L-2014\2014R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\9 L-2013\2013R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\8 L-2012\2012R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\7 L-2011\2011R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\6 L-2010\2010R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\5 L-2009\2009R.dta"
	drop _merge
	save "${datapath}\EU-SILC\16 L-2020\masterR.dta", replace
	clear
	use "${datapath}\EU-SILC\16 L-2020\masterR.dta", clear

	merge 1:1 year upid uhid using "${datapath}\EU-SILC\4 L-2008\2008R.dta"
	drop _merge
	merge 1:1 year upid uhid using "${datapath}\EU-SILC\3 L-2007\2007R.dta"
	drop _merge
	save "${datapath}\EU-SILC\16 L-2020\masterR.dta", replace
	clear
	use "${datapath}\EU-SILC\16 L-2020\masterR.dta", clear

	merge 1:1 year upid uhid using "${datapath}\EU-SILC\2 L-2006\2006R.dta"
	drop _merge
	save "${datapath}\EU-SILC\16 L-2020\masterR.dta", replace
	clear
	use "${datapath}\EU-SILC\16 L-2020\masterR.dta", clear

	merge 1:1 year upid uhid using "${datapath}\EU-SILC\1 L-2005\2005R.dta"
	drop _merge

	/* drop superflous variables*/
	drop db010 db020 db030 db040 db040_f db060 db060_f db062 db062_f db070 db070_f db075 db075_f db095 db095_f db100 db100_f db110 db110_f ///
	nrtgrp* slctd_rtgrp drpout_year drpout_year* slctd_urtgrp* slctd_uhid*  maxgrp lgstgrp drpout_year* ///
    db090 db090_f nrtgrp2013  merge*

	/*adding upidnum*/
	egen upidnum = group(upid)
	save "${datapath}\EU-SILC\16 L-2020\masterR.dta", replace

	/***/
	/*build masterfile for personal data files (P files)*/
	/***/

		/* 2020 */
	clear
	local filen : dir "${datapath}\EU-SILC\16 L-2020\"  files "udb_l20p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\16 L-2020/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid (should be unique)*/
	duplicates report year country pid
	/*merge with R files to keep only obs from selcted rotational groups*/
	merge 1:m year country pid using "${datapath}\EU-SILC\16 L-2020\2020R.dta"
	keep if _merge == 3
	drop _merge
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	duplicates drop year country pid, force
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\16 L-2020\2020P", replace


		/* 2019 */
	clear
	local filen : dir "${datapath}\EU-SILC\15 L-2019\"  files "udb_l19p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\15 L-2019/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid (should be unique)*/
	duplicates report year country pid
	/*merge with R files to keep only obs from selcted rotational groups*/
	merge 1:m year country pid using "${datapath}\EU-SILC\15 L-2019\2019R.dta"
	keep if _merge == 3
	drop _merge
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	duplicates drop year country pid, force
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\15 L-2019\2019P", replace


	/* 2018 */
	clear
	local filen : dir "${datapath}\EU-SILC\14 L-2018\"  files "udb_l18p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\14 L-2018/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid (should be unique)*/
	duplicates report year country pid
	/*merge with R files to keep only obs from selcted rotational groups*/
	merge 1:m year country pid using "${datapath}\EU-SILC\14 L-2018\2018R.dta"
	keep if _merge == 3
	drop _merge
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	duplicates drop year country pid, force
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\14 L-2018\2018P", replace


		/* 2017 */
	clear
	local filen : dir "${datapath}\EU-SILC\13 L-2017\"  files "udb_l17p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\13 L-2017/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid (should be unique)*/
	duplicates report year country pid
	/*merge with R files to keep only obs from selcted rotational groups*/
	merge 1:m year country pid using "${datapath}\EU-SILC\13 L-2017\2017R.dta"
	keep if _merge == 3
	drop _merge
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	duplicates drop year country pid, force
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\13 L-2017\2017P", replace



	/* 2016 */
	clear
	local filen : dir "${datapath}\EU-SILC\12 L-2016\"  files "udb_l16p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\12 L-2016/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear

	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid (should be unique)*/
	duplicates report year country pid
	/*merge with R files to keep only obs from selcted rotational groups*/
	merge 1:m year country pid using "${datapath}\EU-SILC\12 L-2016\2016R.dta"
	keep if _merge == 3
	drop _merge
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	duplicates drop year country pid, force
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\12 L-2016\2016P", replace


	/* 2015 */
	clear
	local filen : dir "${datapath}\EU-SILC\11 L-2015\"  files "udb_l15p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\11 L-2015/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid (should be unique)*/
	duplicates report year country pid
	/*merge with R files to keep only obs from selcted rotational groups*/
	merge 1:m year country pid using "${datapath}\EU-SILC\11 L-2015\2015R.dta"
	keep if _merge == 3
	drop _merge
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	duplicates drop year country pid, force
	/*this is the masterfileR where to add data from previous releases to */
	save "${datapath}\EU-SILC\11 L-2015\2015P", replace

	/*selecting 2014 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\10 L-2014\"  files "udb_l14p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\10 L-2014/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in hid*/
	duplicates report year country pid
	/*merge with IDs from house hold register (R file), keep matches */
	merge 1:m year country pid using "${datapath}\EU-SILC\10 L-2014\2014R.dta"
	keep if _merge == 3
	drop _merge
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\10 L-2014\2014P.dta", replace

	/*selecting 2013 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\9 L-2013\"  files "udb_l13p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\9 L-2013/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid*/
	duplicates report year country pid
	/*merge with IDs from household register (R file), keep the matches */
	merge 1:m year country pid using "${datapath}\EU-SILC\9 L-2013\2013R.dta"
	keep if _merge == 3
	drop _merge
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so drop duplicates from R*/
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\9 L-2013\2013P.dta", replace

	/*selecting 2012 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\8 L-2012\"  files "udb_l12p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\8 L-2012/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in hid*/
	duplicates report year country pid
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	merge 1:m year country pid using "${datapath}\EU-SILC\8 L-2012\2012R.dta"
	keep if _merge == 3
	drop _merge
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\8 L-2012\2012P.dta", replace

	/*selecting 2011 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\7 L-2011\"  files "udb_l11p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\7 L-2011/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in hid*/
	duplicates report year country pid
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	merge 1:m year country pid using "${datapath}\EU-SILC\7 L-2011\2011R.dta"
	keep if _merge == 3
	drop _merge
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\7 L-2011\2011P.dta", replace

	/*selecting 2010 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\6 L-2010\"  files "udb_l10p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\6 L-2010/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in hid*/
	duplicates report year country pid
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	merge 1:m year country pid using "${datapath}\EU-SILC\6 L-2010\2010R.dta"
	keep if _merge == 3
	drop _merge
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\6 L-2010\2010P.dta", replace

	/*selecting 2009 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\5 L-2009\"  files "udb_l09p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\5 L-2009/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in hid*/
	duplicates report year country pid
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	merge 1:m year country pid using "${datapath}\EU-SILC\5 L-2009\2009R.dta"
	keep if _merge == 3
	drop _merge
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\5 L-2009\2009P.dta", replace

	/*selecting 2008 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\4 L-2008\"  files "udb_l08p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\4 L-2008/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	rename pb010 year
	rename pb030 pid
	rename pb020 country
	sort year country pid
	/*checking for duplicates/errors in hid*/
	duplicates report year country pid
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	merge 1:m year country pid using "${datapath}\EU-SILC\4 L-2008\2008R.dta"
	keep if _merge == 3
	drop _merge
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\4 L-2008\2008P.dta", replace

	/*selecting 2007 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\3 L-2007\"  files "udb_l07p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\3 L-2007/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid*/
	duplicates report year country pid
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	merge 1:m year country pid using "${datapath}\EU-SILC\3 L-2007\2007R.dta"
	keep if _merge == 3
	drop _merge
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\3 L-2007\2007P.dta", replace

	/*selecting 2006 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\2 L-2006\"  files "udb_l06p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\2 L-2006/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid*/
	duplicates report year country pid
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	merge 1:m year country pid using "${datapath}\EU-SILC\2 L-2006\2006R.dta"
	keep if _merge == 3
	drop _merge
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\2 L-2006\2006P.dta", replace

	/*selecting 2005 release data*/
	clear
	local filen : dir "${datapath}\EU-SILC\1 L-2005\"  files "udb_l05p_ver*"
	display `filen'
	local filel "${datapath}\EU-SILC\1 L-2005/"
	display `"`filel'"'
	local fileln : display `"`filel'"' `filen'
	use `"`fileln'"', clear


	local new_var=lower("`var'")

    foreach var of varlist _all {
    	local new_var = lower("`var'")
    	cap rename `var' `new_var'
    }

	tostring pb030, replace format("%15.0f")
	gen year = pb010
	gen pid = pb030
	gen country = pb020
	sort year country pid
	/*checking for duplicates/errors in pid*/
	duplicates report year country pid
	/* R file contains combinations in pid and hid, so pids are not unique. In P pids are unique, so we can drop the duplicates */
	merge 1:m year country pid using "${datapath}\EU-SILC\1 L-2005\2005R.dta"
	keep if _merge == 3
	drop _merge
	duplicates drop year country pid, force
	save "${datapath}\EU-SILC\1 L-2005\2005P.dta", replace

	/*merge data from all releases with masterfile.
	!! this process is memory intensive. */



	use "${datapath}\EU-SILC\16 L-2020\2020P.dta", clear
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\15 L-2019\2019P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\14 L-2018\2018P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\13 L-2017\2017P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\12 L-2016\2016P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\11 L-2015\2015P.dta"
	drop _merge

	save "${datapath}\EU-SILC\16 L-2020\masterP.dta", replace
	clear
	use "${datapath}\EU-SILC\16 L-2020\masterP.dta"

	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\10 L-2014\2014P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\9 L-2013\2013P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\8 L-2012\2012P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\7 L-2011\2011P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\6 L-2010\2010P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\5 L-2009\2009P.dta"
	drop _merge

	save "${datapath}\EU-SILC\16 L-2020\masterP.dta", replace
	clear
	use "${datapath}\EU-SILC\16 L-2020\masterP.dta"

	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\4 L-2008\2008P.dta"
	drop _merge
	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\3 L-2007\2007P.dta"
	drop _merge



	save "${datapath}\EU-SILC\16 L-2020\masterP.dta", replace
	clear
	use "${datapath}\EU-SILC\16 L-2020\masterP.dta"

	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\2 L-2006\2006P.dta"
	drop _merge

	save "${datapath}\EU-SILC\16 L-2020\masterP.dta", replace
	clear
	use "${datapath}\EU-SILC\16 L-2020\masterP.dta"

	merge 1:1 year upid country uhid using "${datapath}\EU-SILC\1 L-2005\2005P.dta"
	drop _merge
	/*eliminate variables from the R file*/
	drop rb010 rb010 rb020 rb030 rb040 rb060 rb060_f rb062 rb062_f rb063 rb063_f rb064 rb064_f rb070 rb070_f rb080 rb080_f rb090 rb090_f rb100 ///
	     rb100_f rb110 rb110_f rb120 rb120_f rb140 rb140_f rb150 rb150_f rb160 rb160_f rb170 rb170_f rb180 rb180_f rb190 rb190_f rb200 rb200_f ///
		 rb210 rb210_f rb220 rb220_f rb230 rb230_f rb240 rb240_f rb245 rb245_f rb250 rb250_f rb260 rb260_f rb270 rb270_f rx010 rx020 hid db010 ///
		 db020 db030 db040 db040_f db060 db060_f db062 db062_f db070 db070_f db075 db075_f db095 db095_f db100 db100_f db110 db110_f

	/*drop superflous variables */
	drop drpout_year* slctd_urtgrp* slctd_uhid*  merge* nrtgrp*  lgstgrp maxgrp drpout_year slctd_rtgrp

	/* This is the personal data file (P) file 2003 - 2020 */
	save "${datapath}\EU-SILC\16 L-2020\masterP.dta", replace

	/***/
	/*erasing superflous files from the disc */

	erase "${datapath}\EU-SILC\16 L-2020\2020D.dta"
/*	erase "${datapath}\EU-SILC\16 L-2020\2020H.dta" */
	erase "${datapath}\EU-SILC\16 L-2020\2020P.dta"
	erase "${datapath}\EU-SILC\16 L-2020\2020R.dta"


	erase "${datapath}\EU-SILC\15 L-2019\2019D.dta"
	erase "${datapath}\EU-SILC\15 L-2019\2019H.dta"
	erase "${datapath}\EU-SILC\15 L-2019\2019P.dta"
	erase "${datapath}\EU-SILC\15 L-2019\2019R.dta"

	erase "${datapath}\EU-SILC\13 L-2017\2017D.dta"
	erase "${datapath}\EU-SILC\13 L-2017\2017H.dta"
	erase "${datapath}\EU-SILC\13 L-2017\2017P.dta"
	erase "${datapath}\EU-SILC\13 L-2017\2017R.dta"

	erase "${datapath}\EU-SILC\12 L-2016\2016D.dta"
	erase "${datapath}\EU-SILC\12 L-2016\2016H.dta"
	erase "${datapath}\EU-SILC\12 L-2016\2016P.dta"
	erase "${datapath}\EU-SILC\12 L-2016\2016R.dta"

	erase "${datapath}\EU-SILC\11 L-2015\2015D.dta"
	erase "${datapath}\EU-SILC\11 L-2015\2015H.dta"
	erase "${datapath}\EU-SILC\11 L-2015\2015P.dta"
	erase "${datapath}\EU-SILC\11 L-2015\2015R.dta"

	erase "${datapath}\EU-SILC\10 L-2014\2014D.dta"
	erase "${datapath}\EU-SILC\10 L-2014\2014H.dta"
	erase "${datapath}\EU-SILC\10 L-2014\2014P.dta"
	erase "${datapath}\EU-SILC\10 L-2014\2014R.dta"

	erase "${datapath}\EU-SILC\9 L-2013\2013D.dta"
	erase "${datapath}\EU-SILC\9 L-2013\2013H.dta"
	erase "${datapath}\EU-SILC\9 L-2013\2013P.dta"
	erase "${datapath}\EU-SILC\9 L-2013\2013R.dta"

	erase "${datapath}\EU-SILC\8 L-2012\2012D.dta"
	erase "${datapath}\EU-SILC\8 L-2012\2012H.dta"
	erase "${datapath}\EU-SILC\8 L-2012\2012P.dta"
	erase "${datapath}\EU-SILC\8 L-2012\2012R.dta"

	erase "${datapath}\EU-SILC\7 L-2011\2011D.dta"
	erase "${datapath}\EU-SILC\7 L-2011\2011H.dta"
	erase "${datapath}\EU-SILC\7 L-2011\2011P.dta"
	erase "${datapath}\EU-SILC\7 L-2011\2011R.dta"

	erase "${datapath}\EU-SILC\6 L-2010\2010D.dta"
	erase "${datapath}\EU-SILC\6 L-2010\2010H.dta"
	erase "${datapath}\EU-SILC\6 L-2010\2010P.dta"
	erase "${datapath}\EU-SILC\6 L-2010\2010R.dta"

	erase "${datapath}\EU-SILC\5 L-2009\2009D.dta"
	erase "${datapath}\EU-SILC\5 L-2009\2009H.dta"
	erase "${datapath}\EU-SILC\5 L-2009\2009P.dta"
	erase "${datapath}\EU-SILC\5 L-2009\2009R.dta"

	erase "${datapath}\EU-SILC\4 L-2008\2008D.dta"
	erase "${datapath}\EU-SILC\4 L-2008\2008H.dta"
	erase "${datapath}\EU-SILC\4 L-2008\2008P.dta"
	erase "${datapath}\EU-SILC\4 L-2008\2008R.dta"

	erase "${datapath}\EU-SILC\3 L-2007\2007D.dta"
	erase "${datapath}\EU-SILC\3 L-2007\2007H.dta"
	erase "${datapath}\EU-SILC\3 L-2007\2007P.dta"
	erase "${datapath}\EU-SILC\3 L-2007\2007R.dta"

	erase "${datapath}\EU-SILC\2 L-2006\2006D.dta"
	erase "${datapath}\EU-SILC\2 L-2006\2006H.dta"
	erase "${datapath}\EU-SILC\2 L-2006\2006P.dta"
	erase "${datapath}\EU-SILC\2 L-2006\2006R.dta"

	erase "${datapath}\EU-SILC\1 L-2005\2005D.dta"
	erase "${datapath}\EU-SILC\1 L-2005\2005H.dta"
	erase "${datapath}\EU-SILC\1 L-2005\2005P.dta"
	erase "${datapath}\EU-SILC\1 L-2005\2005R.dta"

	/**/
	display "Rescaling weights in R file."
	/*import the total population for each country from eurostat*/
	/*total population*/
	clear
	findfile totalpopulation_2003_2020.dta, path(BASE;SITE;.;PERSONAL;PLUS)
	return list
	use`"`r(fn)'"'
	rename B a2003
	rename C a2004
	rename D a2005
	rename E a2006
	rename F a2007
	rename G a2008
	rename H a2009
	rename I a2010
	rename J a2011
	rename K a2012
	rename L a2013
	rename M a2014
	rename N a2015
	rename O a2016
	rename P a2017
	rename Q a2018
	rename R a2019
	rename S a2020



	reshape long a, i(country) j(year)
	rename a pop
	destring pop, replace
	drop if pop == .
	merge 1:m  country year using "${datapath}\EU-SILC\16 L-2020\masterR.dta"
	drop if _merge == 1
	drop _merge

	/*now calculate scales for the base weights for individuals in R file rscale*/
	bysort year country: egen  wcountry = total (rb060)
	bysort year country urtgrp: egen wgroup = total( rb060 )
	gen rscale = ( wgroup / wcountry )
	gen rb060s = rscale * rb060
			/*check scaled weight*/
			gen test1 = ( wcountry - pop ) / pop
			bysort year country: egen test2 = total (rb060s)
			gen smwrate60 = ( test2 - pop ) / pop
			/*what does this do?*/
			tab country , summarize (smwrate60)
			tab year, summarize (rscale)
			drop test1 test2 wgroup wcountry

	/*extend presence of rb064 to all observations pertaining to a certain unit*/
	bysort upid : egen lrb064 = max(rb064)
	bysort year country: egen  lwcountry = total (lrb064)
	bysort year country urtgrp: egen lwgroup = total( lrb064 )
	gen lrscale = ( lwgroup / lwcountry )
	gen rb064s = lrscale * lrb064
			/*check scaled weight*/
			gen test1 = ( lwcountry - pop ) / pop
			bysort year country: egen test2 = total (rb064s)
			gen smwrate64 = ( test2 - pop ) / pop
			/*what does this do?*/
			tab country , summarize (smwrate64)
			tab year, summarize (lrscale)
			drop test1 test2 lwgroup lwcountry

		save "${datapath}\EU-SILC\16 L-2020\masterR.dta", replace

/**/
	display "Rescaling weights in P file."
	/*import the total population for each country from eurostat*/
	/*total population*/
	clear
	findfile totalpopulation_2003_2020.dta, path(BASE;SITE;.;PERSONAL;PLUS)
	return list
	use`"`r(fn)'"'
	rename B a2003
	rename C a2004
	rename D a2005
	rename E a2006
	rename F a2007
	rename G a2008
	rename H a2009
	rename I a2010
	rename J a2011
	rename K a2012
	rename L a2013
	rename M a2014
	rename N a2015
	rename O a2016
	rename P a2017
	rename Q a2018
	rename R a2019
	rename S a2020


	reshape long a, i(country) j(year)
	rename a pop
	destring pop, replace
	drop if pop == .
	merge 1:m  country year using "${datapath}\EU-SILC\16 L-2020\masterP.dta"
	drop if _merge == 1
	drop _merge

	/*now calculate scales for the base weights for individuals in R file rscale*/
	bysort year country: egen  wcountry = total (pb080)
	bysort year country urtgrp: egen wgroup = total( pb080 )
	gen pscale = ( wgroup / wcountry )
	gen pb080s = pscale * pb080
			/*check scaled weight*/
			gen test1 = ( wcountry - pop ) / pop
			bysort year country: egen test2 = total (pb080s)
			gen smwrate80 = ( test2 - pop ) / pop
			/*what does this do?*/
			tab country , summarize (smwrate80)
			tab year, summarize (pscale)
			drop test1 test2 wgroup wcountry
	save "${datapath}\EU-SILC\16 L-2020\masterP.dta", replace
	display "The files masterP.dta, masterR.dta, masterH.dta and masterD.dta are ready for use and can be found in ${datapath}\EU-SILC\16 L-2020\ " as result
end