Hi everyone,
Using Stata 15.1 MP8, I've encountered a variety of errors that seem to be caused by calling on Mata within a Stata if command that is evaluated as false. Try this:
loc run="no"
if "`run'"=="yes" {
mata {
test=1
test
}
}
else di "working fine"
The error returned is:
} is not a valid command name
Obviously the brackets are being treated in a specific way, as if the closing Mata bracket is being ignored? Some evidence for this is found with the following code, which works by excluding one of the closing brackets:
loc run="no"
if "`run'"=="yes" {
mata {
test=1
test
}
else di "working fine"
Furthermore, when the statement is evaluated as true, everything is fine. This, of course, can be shown by starting the first set of code with: loc run="yes".
The problem here can be significant, such as in my case. I'm coding a variety of analysis procedures that combine Mata and Stata, and I want to give users the option to skip over large sections of the code. For this, I'm using locals at the start of the .do file that can be set as "yes" or "no" and then evaluated as true or false in order to skip over lots of Mata and Stata code (e.g., if "`run_code'"=="yes" { ... ).
Any help here is greatly appreciated! Apologies if this issue has been addressed elsewhere. I've been looking around for a few days on-and-off, but couldn't find this issue addressed.
Mike
Related Posts with When Stata if command is false, calling on Mata causes errors
Drop observations corresponding to other variableI have two variables, Names1 and Names2, both consisting of certain names. A simplified e.g. is past…
Several updates and new packages on SSC (heatplot, colrspace, palettes, robstat, robbox)I made several additions and updates to packages on SSC over the last months about which I never inf…
Diff-in-Diff Regression for Minimum Wage ImpactsHi all, I'm very new to Stata and I'm doing my own research for a class to write a paper. I'm trying…
Is there a Mata function for inputting to and outputting from local macros in the calling segment?Fellow Statalisters (especially StataCorp): Are there any Mata functions to input values from, and …
ivreghdfe interaction term!Hello, I'm using ivreghdfe and need to interact my instrument with a categorical variable. Could so…
Subscribe to:
Post Comments (Atom)
0 Response to When Stata if command is false, calling on Mata causes errors
Post a Comment