Dear forum,
I have a number of datasets, each consisting of various years. On each file I run the same DiD regressions. The files have identical structure and only differ in their interaction variables' naming. For example, a data set for the years 2004-2009 with DiD from 2007 has variables named ocpagr2007, ocpind2007 etc.

I want Stata to infer the year of the interaction from the variable name. The following has failed:
Code:
ds ocpagr2*
local interaction_year substr(r(varlist), 7, 8)
display `interaction_year'

/*Some other code
..
..*/

reg lrealhrlywage age agesqr schooly schoolysqr married male muslim ocpagr* ocpcns* ocpind* i.yearsur [pweight=weight] if occup1d=="9" & outlier==0

 test ocpagr = ocpagr`interaction_year'
I have received the following error:
Code:
unknown function ocpagrsubstr()
r(133);
Despite the fact that the "display `interaction_year'" above does print "2007". Also, when I manually write the bottom line as " test ocpagr = ocpagr2007", it works.
I suspect that the macro doesn't store the literal value of "r(varlist)", and so the value is changed/"emptied" after I go on with running some other code.

I did a fare share of googling but to couldn't figure out how to overcome this issue. Thanks a lot!
Best,
Yuval