Reputation: 8768
I would like to count observations of a variable made of observations of other variables which fulfill logical criteria and use it in an assert
statement in order to do some testing of data constraints, e.g.
generate myvar = (var1 == 0 & var2 > 0) | (var1 == 0 & var3b > 0)
assert magiccommand(myvar) == 0
where magiccommand
should be the command or function I'm looking for (doesn't have to be a function).
I tried
count
: help count
doesn't provide any information on how to count observations. Linkage of information in general is very poor.sum()
: doesn't do the job, the graphical output displays the number of observations, but Stata's poor language concept doesn't allow programmatic retrieval of the value, nor does it provide any points of entry for information retrieval in the manual via help
.by
: is insufficiently explained in general and seems to be designed too powerful.assert
and still too shocked about the complexity of the command for such a ridiculously easy task.help _n
: "contains the number of the current observation" without a link to an explanation what the current observation is...I'd be very interested in a way how I could have found out an answer myself because it's very hard to use closed source software which is built on selling expensive books compensating the malious manual.
I have to use Stata
13 on Windows 7.
Upvotes: 0
Views: 877
Reputation: 11102
You can use summarize
:
clear
set more off
input ///
var1 var2 var3b
0 1 5
0 0 0
0 0 3
1 0 0
end
generate myvar = (var1 == 0 & var2 > 0) | (var1 == 0 & var3b > 0)
list
*-----
summarize myvar
assert r(sum) == 0
Another way is
<snip>
count if myvar
assert r(N) == 0
Yet another:
<snip>
gen c = sum(myvar)
assert c[_N] == 0
help return
is one reference. help subscripting
is another one.
search count observations
takes you to count
, which is very clear on what it does, including examples. I really don't understand why you say it "doesn't provide any information on how to count observations". See also
The Stata Journal, Volume 7 Number 1: pp. 117-130, Speaking Stata: Making it count, by Nicholas J. Cox (http://www.stata-journal.com/article.html?article=pr0029).
With all due respect, I think you've done a really poor job of reading the help
files and the manuals. You really don't explain what you mean by length()
in your post. You assume that Stata's programming paradigm is equal to those of other languages, and because you haven't studied it, you assume it is a bad one. It's different, I agree. It does some things fine, others not so fine, but so do all other languages. Stata developers have made it possible to seek help in many ways (really). This is clearly explained in the very first sections of the User's Manual.
If I've given code that doesn't solve your problem, then you probably need to explain yourself better.
Upvotes: 2