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
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: How can I count the number of variables that meet a certain condition per observation?
Date
Sat, 3 Dec 2011 15:35:40 +0000
In addition to Daniel's reply
gen count = 0
gen sum = 0
forval j = 101/110 {
replace count = count + ( var'j' > 1 )
replace sum = sum + var'j' if var'j' > 1
}
where ' ' are to be understood as left single quote ... right single
quote.
Nick
On 3 Dec 2011, at 11:49, "Schreck, Philipp" <[email protected]> wrote:
Dear list members,
Say my dataset includes 10 observations and 10 variables, var101-
var110. Entries can take values 0, 1, or 2 (I use Stata/SE 11).
I would like to count for each observation the number of variables
that meet a certain condition, say var*==2. Although I searched the
list, I cannot find the appropriate command and would be very
grateful for any suggestions. I could loop over var101-var110 and
(for each var) generate a new variable with value "1" if the
respective variable is greater than 1 (and then use -rsum- with the
new variables). But that seems overly complicated and I was
wondering whether there's an easy command I've been missing.
Maybe it could be something like:
egen varcount = rowtotal (var101-var110)
but then I get the sum of all values (across vars) per observation
which is not what I want.
Is there a simple way of computing the sum across variables (for
each observation) ONLY for those variables with values greater than
1 (or, alternatively, the number of variables with values greater
than 1 per observation)?
*
* 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/