Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: How can I count the number of variables that meet a certain condition per observation?


From   daniel klein <[email protected]>
To   [email protected]
Subject   Re: st: How can I count the number of variables that meet a certain condition per observation?
Date   Sun, 4 Dec 2011 14:30:09 +0100

Phillip,

using Nick's code would in this case probably be best, as it creates
both quantities in one run and I think it might be a bit faster than
using -egen-. However in the special case you reported, .i.e. your 10
variables do only take values 0, 1 or 2, then you can easily get the
desired sum multiplying  -egen-'s -anycount- result by 2.

egen byte count = anycount(var_101-var_110) ,v(2)
g byte sum = 2 * count if count

Best
Daniel

-- 
[...]
I actually needed both, counting the number of variables with value "2":
egen var1 = anycount(var_101-var_110), values(2)

and computing the sum across these variables (sorry for the imprecise question).
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index