I have smallholder agriculture plot level commercialization data which i want to collapse to household level data. To avoid losing variable labels i resorted to the foreach loop method below by Cox;

Copy variable labels before collapse . foreach v of var * { . local l`v' : variable label `v' . if `"`l`v''"' == "" { . local l`v' "`v'" . } . } Attach the saved labels after collapse​​​​​​​ . foreach v of var * { . label var `v' "`l`v''" . }
Whenever i try running this codes, i am getting the error message:

"foreach command may not result from a macro expansion interactively or in do files"


What could be the reason? Thank you in advance

​​​​​​​Takesure Tozooneyi