Why doesn’t this do-file run through …?

Quite often a do-file is written to run on various data-files that all seem to be the same. Say, you have done it for one year of a data-set and want to repeat the same for the subsequent years that you have data. Now, a check whether the data actually has some data (given your selection) is often a good idea, here is how I did it in a recent project:


count
assert `r(N)'>1

I am using the saved return local variable r(N) that STATA automatically generates after count. This is a need feature that you should consider for many other commands (try return list after your favourite command).

In this case I “assert” that there are more than one observation in the data-set, becaus `r(N)’ simply gives the count value. If this condition is not fulfilled STATA stops the do-file. You can vary this, by doing e.g. the following


count if age==30
if `r(N)'< 10 {
... do something ...
}

So a certain action is taken only if the number of observation is less than 10.

Leave a Reply