I have a situation where I need to use values in two variables mpg and make to create a graph. The variable mpg is a numerical variable and make is a string variable with spaces. The following code is able to use the values in mpg to create the graph when I ignore the values in the make variable. However I would like to have the corresponding string value from variable make appear in the graph subtitle. For some reason this code gives the error too many ')' or ']' after looping over several of the values in the variable mpg
sysuse auto.dta, clear
gsort - trunk
keep in 1/5
levelsof mpg, local(levels)
global xlist `levels'
levelsof make, local(levelsdes)
global ylist `levelsdes'
sysuse auto.dta, clear
gen bar=1
local k 1 2 3 4 5
local n : word count $xlist
forvalues x =1/`n' {
local i : word `x' of $xlist
local z : word `x' of `k'
local j : word `x' of $ylist
egen a_`i'= total(bar) if mpg == `i'
twoway (bar a_`i' mpg ), subtitle(`j') title(Trends in Error `i')
graph export "`z'.png", as(png) replace
}