Does Stata have any `try and catch` mechanism similar to Java?
Asked Answered
C

3

10

I am writing a .do to check the existence of some variables in a number of .dta files as well as to check the existence of certain values for those variables. However, my code stops executing as it encounters an invalid variable name.

I know I mix Java and Stata coding, and it is completely inappropriate, but is there any way I could do something like:

try {
su var1
local var1_mean=(mean)var1
local var1_min=(min)var1
local var1_max=(max)var1
...
}
catch (NoSuchVariableException e) {
System.out.println("Var1 does not exist")
}
// So that the code does not stop executing...?
Conchology answered 2/6, 2013 at 15:46 Comment(0)
D
24

The short answer is Yes. A slightly longer answer is that guessing what the syntax might be by analogy with Java has minimal chance of success. It is best to read Stata's documentation, e.g. start by skimming the main entries in the [P] manual.

Here the problem being trapped is that no var1 exists. This code is legal, or so I trust:

capture su var1, meanonly 

if _rc == 0 { 
     local var1_mean = r(mean)
     local var1_min  = r(min)
     local var1_max  = r(max)
}
else display "var1 does not exist"

The idea is two-fold. capture eats any error of the command it prefixes, but a return code will still be accessible in _rc. Non-zero return codes are error codes.

A related command is confirm, e.g.

capture confirm var var1 

checks that a variable var1 exists.

Dyane answered 2/6, 2013 at 16:5 Comment(4)
Thanks a lot. I actually have been searching the Stata manuals for the keyword try...i thought that this should be a natural way of checking a variable existence (maybe b/c i am more familiar w/java)...then i just got tired and frustrated...so i decided to ask REAL PROFESSIONALs :)Conchology
Stata predates Java in first origin, so to a good approximation any similarity of syntax must reflect accident or common sources of inspiration. But Stata never promised to be anything else; it uses its own words for many things.Dyane
A less verbose alternative would be "capture noisily." This also has the advantage of not having to supply your own error explanations.Hexagonal
@Hexagonal That would be indeed be shorter. The larger question is that the shorter code you are suggesting just aborts the program on error, while the answer (a) explains the construct that the OP seems to be asking for in the question and (b) points the way to branching code, which here and more importantly elsewhere may be what is the bigger goal.Dyane
C
2

You can also prevent the execution of a do file to stop when a error occurs by adding the nostop option to the call:

do myfile, nostop

Crustacean answered 3/5, 2018 at 8:42 Comment(1)
Correct. I have to say that I have never seen much point to this feature. It's a good idea if and only if a failure has no consequences for anything that follows, which is in my experience unusual for any substantial code.Dyane
F
0

One way is to simply insert your code in the command line. Note: you need to prepare it first, and then to copy paste it into the command line. Let's say you have two variables, var1 && var2, and var1 does not exist for your first file, then:

Option 1. your .do file is:

su var1
su var2
...

As you try to execute it you would get the following: variable var1 is not found //and that's all the code stopped

Option 2. you can copy paste the same line into the command field:

su var1
su var2
...

The result is:

. variable var1 is not found
. sum var2

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
       var2 |     5              39     26             1         8

. 
Fao answered 2/6, 2013 at 16:5 Comment(2)
The proposal here appears to be that Stata will tell you if your code makes incorrect assumptions. That is so, but I doubt that solves the problem posted. In particular, do-files by default stop as soon as there is an error. What happens interactively is not the issue.Dyane
This solution is not exactly solving my problem :(Conchology

© 2022 - 2024 — McMap. All rights reserved.