Hello, anyone have an idea why Im getting an invalid syntax error right at the end of this loop?
Thanks :D
Code:
* declare local macros for ownership names
local name1 "SOEs"
local name2 "PRIs"
local name3 "FIEs"
* loop over ownership categories
forvalues o=1/3 {
* loop through entry year 2001 to 2016
forvalues y=2001 {
* open the dataset
use "$output/Temp.dta", clear
* restrict to firms whose first year in the data is the current entry year
egen firstyear=min(year), by(madn)
keep if firstyear==`y'
* keep if the firm is the current ownership type in entry year
gen temp=owner if year==`y'
egen owner`y'=max(temp), by(madn)
drop temp
keep if owner`y'==`o'
* restrict to firms in manufacturing in entry year
gen temp=vsic1993 if year==`y'
egen vsic`y'=max(temp), by(madn)
drop temp
keep if vsic`y'>1500 & vsic`y'<3700
* create a variable that tracks end of year employment during the entry year
gen temp=empend if year==`y'
egen empend`y'=max(temp), by(madn)
drop temp
* calculate mean initial and contemporary employment among survivors by year
collapse (mean) empend empend`y', by(year)
label variable year "Year"
* create a line graph of the two mean employment series
* if SOEs
if `o'==1 {
line empend year, lcolor(red) || line empend`y' year, lcolor(blue) lpattern(dash) ///
ytitle("") xlabel(2001(2)2017) ///
graphregion(color(white)) ylabel(0(100)600, angle(0)) legend(rows(1) label(1 "Contemporary employment") label(2 "Initial employment"))
}
* if PRIs
if `o'==2 {
line empend year, lcolor(red) || line empend`y' year, lcolor(blue) lpattern(dash) ///
ytitle("") xlabel(2001(2)2017) ///
graphregion(color(white)) ylabel(0(20)100, angle(0)) legend(rows(1) label(1 "Contemporary employment") label(2 "Initial employment"))
}
* if FIEs
if `o'==3 {
line empend year, lcolor(red) || line empend`y' year, lcolor(blue) lpattern(dash) ///
ytitle("") xlabel(2001(2)2017) ///
graphregion(color(white)) ylabel(0(200)800, angle(0)) legend(rows(1) label(1 "Contemporary employment") label(2 "Initial employment"))
}
* export the graph to pdf
graph export "$tabfig/mean_employment_surviving_`name`o''_`y'entry.pdf", as (pdf) replace
* calculate relative to entry year
gen sempend=empend/empend`y'
* save the dataset
gen startyear=`y'
gen owner=`o'
save "$temp\Mean employment among surviving `name`o'' `y' entrants.dta", replace
}
* end of looping through entry years 2001 to 2016
* append over the years
use "$temp/Mean employment among surviving `name`o'' 2001 entrants.dta", clear
forvalues y=2002/2016 {
append using "$temp/Mean employment among surviving `name`o'' `y' entrants.dta"
}
* save the dataset
save "$temp/Mean employment among surviving `name`o'' entrants.dta", replace
}
* end of looping through ownership

Comment