This is just an illustrative example to explain the problem. The following code, which combines stata and mata with an if loop inside it, works perfectly. In the first block, we delete the missing values from a matrix, in the second, we do not.
cap program drop myexample
program def myexample,rclass
version 14
syntax varlist [ , IGnore ]
args var1
tempname rn A check index
local var1: word 1 of `varlist'
quietly desc `var1'
local rn=r(N)
mkmat `var1',mat(`A')
mata{
st_matrix(st_local("check"),rowmissing(st_matrix("`A'")):== 0)
if(colsum(st_matrix("`check'")):!=`rn'){
st_matrix(st_local("index"),selectindex(st_matrix("`check'")))
st_matrix(st_local("A"),st_matrix("`A'")[st_matrix("`index'"),.])
}
}
mat li `A'
end
However, suppose that we have to introduce this mata code inside a block, such that the previous code is altered as follows:
if "`ignore'" != "" {
mkmat `var1',mat(`A')
mata{
st_matrix(st_local("check"),rowmissing(st_matrix("`A'")):== 0)
if(colsum(st_matrix("`check'")):!=`rn'){
st_matrix(st_local("index"),selectindex(st_matrix("`check'")))
st_matrix(st_local("A"),st_matrix("`A'")[st_matrix("`index'"),.])
}
}
}
else mkmat `var1',mat(`A')
If you type in your console myexample yourvariable
, you will get the error } is not a valid command name
. However, you get the expected result when you enter into the ignore
block, that is, myexample yourvariable, ig
. It looks like Stata has problems to identify mata{}
when inside a block.
So, instead of using braces inside the block I tried the following:
if "`ignore'" != "" {
mkmat `var1',mat(`A')
mata: st_matrix(st_local("check"),rowmissing(st_matrix("`A'")):== 0)
mata: if(colsum(st_matrix("`check'")):!=`rn'){
mata: st_matrix(st_local("index"),selectindex(st_matrix("`check'")))
mata: st_matrix(st_local("A"),st_matrix("`A'")[st_matrix("`index'"),.])
mata: }
mata: ; /*Semicolon introduced to indicate there is no else statement */
}
else mkmat `var1',mat(`A')
Now the problem arises from the if inside mata. When you type in your console myexample yourvariable,ig
, you get the error unexpected end of line <istmt> incomplete
.
I would like to know if there is a way to introduce mata code with an if loop inside a block.
Aucun commentaire:
Enregistrer un commentaire