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]

From |
Nick Cox <n.j.cox@durham.ac.uk> |

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

Subject |
st: RE: RE: Problem with constructing a loop for egen anycount |

Date |
Sat, 21 May 2011 18:27:41 +0100 |

foreach ... in ... should be foreach ... of , throughout. Nick n.j.cox@durham.ac.uk Nick Cox -egen-'s -anycount()- function can be traced at least back to the -neqany()- function published in STB-50 in 1999 for Stata 6. (My dim recollection is that _that_ arose from a Statalist query in the late 1990s, so we are near full circle.) At that point, Stata had no -forval- or -foreach- commands and it was a slightly bigger deal to approach problems like yours. Let's forget history and look at it directly. First initialise a count variable gen count3 = 0 Let's give your numlist 1(16)241 to -foreach- and solve the problem for -personid- 1. qui foreach j in num 1(16)241 { replace count3 = count3 + (dhcr`j' == 3) if personid == 1 } For all the personids, we need something more like forval i = 1/16 { local J = 240 + `i' foreach j in num `i'(16)`J' { replace count3 = count3 + (dhcr`j' == 3) if personid == 1 } } If your condition is more complicated than (dhcr`j' == 3) you just need to change that to whatever it is. Think about using -inlist()- or -inrange()- if it's complicated. In short, I suggest gen count3 = 0 forval i = 1/16 { local J = 240 + `i' foreach j in num `i'(16)`J' { replace count3 = count3 + (dhcr`j' == 3) if personid == 1 } } Nick n.j.cox@durham.ac.uk Eleonora Paesen I was wondering if you could help me to create a loop for egen anycount The aim is to create a single variable counting the number of values in a sequence of variables. However, the sequence of variables to be counted vary systemtically dependent on a person identifier variable (personid) which is an integer from 1 to 16. for example For people with personid 1 the sequence of variables would be "dhcr01 dhcr17 dhcr33 dhcr49 dhcr65 dhcr81 dhcr97…dhcr241" For people with personid 2 the sequence of variables would be "dhcr02 dhcr18 dhcr34 dhcr50 dhcr66 dhcr82 dhcr98…dhcr242" … Up to people with personid 16 "dhcr16 dhcr32 dhcr48 dhcr64 dhcr80 dhcr96 dhcr111..dhcr256" i.e the first number of the variable in a sequence is the number of the group and then variables follow at 16 apart. The long way round to this would be to generate count variables for each for each person id, e.g. egen count1 = anycount("Sequence for personid==1') if perid==1 , values (3) egen count2 = anycount("Sequence for personid==2') if perid==2 , values (3) etc and then combine them those variables using the replace command but i presume that there are more elegant solutions. I have tried the lines below without success: forvalues i = 1(16)257 { local a 1 while `a'<17 { local person`a' "dhcr`i'" foreach x of local person`a' { egen temp`a' = anycount(`x') if perid==`a' , values (3) } local a=`a'+1 } } * * 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/

**Follow-Ups**:**Re: st: RE: RE: Problem with constructing a loop for egen anycount***From:*Eleonora Paesen <eleonora.paesen@googlemail.com>

**References**:**st: Problem with constructing a loop for egen anycount***From:*Eleonora Paesen <eleonora.paesen@googlemail.com>

**st: RE: Problem with constructing a loop for egen anycount***From:*Nick Cox <n.j.cox@durham.ac.uk>

- Prev by Date:
**st: RE: Problem with constructing a loop for egen anycount** - Next by Date:
**st: forcing non-negative parameters in OLS** - Previous by thread:
**st: RE: Problem with constructing a loop for egen anycount** - Next by thread:
**Re: st: RE: RE: Problem with constructing a loop for egen anycount** - Index(es):