I have a list of variables that I want to loop through:

yr2008_q110atte
yr2009_q112atte
yr2010_q112atte
yr2011_q110atte
yr2012_q110atte

As you can see, they all start with "yr" and ends with "atte", although the question number in each year is different.

So, I write the loop below:

foreach i in 2008 2009 2010 2011 2012{
tab yr`i'_*atte yr`i'_age_grp
gen yr`i'_atte = . if yr`i'*atte == 1
gen yr`i'_atte = 1 if yr`i'*atte == 1
replace yr`i'_atte = 0 if yr`i'*atte == 1
}

The first line of tabulating variables works, but starting from the second line within the loop, the yr`i'*atte condition doesn't work, as stata returns "yr2008 ambiguous abbreviation".

Any clue on how to resolve this? Thank you!