Bookmark and Share

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: RE: RE: Problem with constructing a loop for egen anycount


From   Eleonora Paesen <[email protected]>
To   [email protected]
Subject   Re: st: RE: RE: Problem with constructing a loop for egen anycount
Date   Mon, 23 May 2011 11:00:42 +0100

Thanks Nick!
The following command works perfectly!

gen count3 = 0

qui forval i = 1/16 {
	local J = 240 + `i'
	foreach j of num `i'(16)`J' {
		replace count3 = count3 + (dhcr`j' == 3) if perid == `i'
	}
}

Best,
Eleonora

On Sat, May 21, 2011 at 6:27 PM, Nick Cox <[email protected]> wrote:
> foreach ... in ... should be foreach ... of , throughout.
>
> Nick
> [email protected]
>
> 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
> [email protected]
>
> 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/
>

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