Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

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


From   "Schreck, Philipp" <schreck@bwl.lmu.de>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: How can I count the number of variables that meet a certain condition per observation?
Date   Sat, 3 Dec 2011 12:49:18 +0100

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)?

Thank you very much for your consideration, I appreciate any suggestions.

Philipp



*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index