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]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

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 }

Nick On 3 Dec 2011, at 11:49, "Schreck, Philipp" <schreck@bwl.lmu.de> 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 variablesthat meet a certain condition, say var*==2. Although I searched thelist, I cannot find the appropriate command and would be verygrateful for any suggestions. I could loop over var101-var110 and(for each var) generate a new variable with value "1" if therespective variable is greater than 1 (and then use -rsum- with thenew variables). But that seems overly complicated and I waswondering 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 observationwhich is not what I want.Is there a simple way of computing the sum across variables (foreach observation) ONLY for those variables with values greater than1 (or, alternatively, the number of variables with values greaterthan 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/

**References**:**st: How can I count the number of variables that meet a certain condition per observation?***From:*"Schreck, Philipp" <schreck@bwl.lmu.de>

- Prev by Date:
**R: st: Re:** - Next by Date:
**st: Fw: Event within dates** - Previous by thread:
**st: How can I count the number of variables that meet a certain condition per observation?** - Next by thread:
**Re: st: How can I count the number of variables that meet a certain condition per observation?** - Index(es):