As shown in the code below, a chunk of Mata code in a Stata do file works well. If you create a Stata program, a single line of Mata code works inside the Stata program, but if you try to nest a chunk of Mata code inside the Stata program, it gives an error. I think Stata mixes the "end" of the Mata chunk with the "end" of the Stata program. I know that it is possible to take the chuck of Mata code outside the Stata program and write it as a Mata function to be called inside the Stata program in a single Mata line. But is it really the only way to do it, or is there a simple trick that I am not aware of to get the chunk of Mata code work right inside the Stata program?
Code:
//chunk of Mata code works here
mata
for(i=1;i<=10;i++) {
A=i
B=i+1
}
end
cap program drop yeah
program yeah
//
//chunk of Stata code here
//
//single line of Mata code works here
mata: C=1
//chunk of Mata code inside the Stata program doesn't work
mata
for(i=1;i<=10;i++) {
D=i
E=i+1
}
end //to end the Mata chunk above
end //to end the Stata program
yeah

Comment