How do you refer to the local i of the mata for loop in a st_ function such as st_numscalar? I tried a few things in the commented section below including st_numscalar("`A'i") but nothing worked, and I couldn't find an example or solution anywhere online.
Code:
tempname A1
tempname A2
tempname mat
mata
st_numscalar("`A1'", 4)
st_numscalar("`A2'", 5)
`mat' = J(2,1,0)
// This for loop doesn't work to create a 1x2 matrix with A1 and A2 values
for(i=1;i<=2;i++) {
`mat'[i,1] = st_numscalar("`A'i")
}
`mat'
end


Thinking outside the box produced a solution! Thank you so much, Niels! You're so great! So, for all other people viewing this post, going back to the very first code that I posted, the solution is: when you need to use the i of a for-loop in Mata as a piece of string, instead of writing a Mata for-loop, wrap a Stata for-loop around your Mata code and use the `i' in your Mata lines. I agree that there may be a smarter way to do this, or perhaps a need to be able to do this purely in Mata, but for now, the solution looks like this:
Comment