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

# st: Problem with constructing a loop for egen anycount

 From Eleonora Paesen To statalist@hsphsun2.harvard.edu Subject st: Problem with constructing a loop for egen anycount Date Sat, 21 May 2011 17:15:20 +0100

```Hello,

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
}
}

Any suggestion or help would be greatly appreciated!!

Best,

Eleonora

*
*   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/
```