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...?
CHEBURASHKA
  • 1,623
  • 11
  • 53
  • 85

3 Answers3

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.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • 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 :) – CHEBURASHKA Jun 02 '13 at 16:15
  • 2
    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. – Nick Cox Jun 02 '13 at 16:28
  • A less verbose alternative would be "capture noisily." This also has the advantage of not having to supply your own error explanations. – biased_estimator Mar 20 '15 at 11:52
  • @biased_estimator 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. – Nick Cox Mar 20 '15 at 11:59
1

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

Abramodj
  • 5,709
  • 9
  • 49
  • 75
  • 3
    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. – Nick Cox May 03 '18 at 11:52
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

. 
Buras
  • 3,069
  • 28
  • 79
  • 126
  • 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. – Nick Cox Jun 02 '13 at 16:09
  • This solution is not exactly solving my problem :( – CHEBURASHKA Jun 02 '13 at 16:16