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]

Re: st: Summarizing properties of other group members meeting specified conditions


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Summarizing properties of other group members meeting specified conditions
Date   Tue, 28 Jun 2011 08:20:57 +0100

There was another typo in the line I corrected (you can't have two
-if-s) and it is not clear where your r(max) comes from. This may be
closer, but I've not tested it.

gen maxwins = .
su org_id, meanonly
quietly forvalues i = 1/`r(max)' {
    gen include 1 = if org_id != `i' & cond_1 == 1
    egen x = max(var_2 * include) if cond_2 == 1, by(project_id)
    replace maxwins = x if org_id == `i'
    drop include x
}



On Mon, Jun 27, 2011 at 3:26 PM, Erik Aadland <erikaadland@hotmail.com> wrote:
> Dear statalist.
>
> After having read the following FAQ http://www.stata.com/support/faqs/data/members.html and experimented with solutions, I am still struggling.
>
> For each project_id, I am trying to generate a variable that for each org_id that meets a specified condition generates the highest observed value on a given variable for another org_id that meets a different specified condition. I think an example is in order:
>
> Example structure:
>
> project_id    org_id    cond_1    cond_2    cond_3    var_1    var_2
> 1             1         0         1         0         3        0
> 1             2         1         0         0         0        3
> 1             3         0         0         1         0        1
> 2             4         0         1         0         2        0
> 2             4         1         0         0         0        4
> 2             5         1         0         0         0        3
> 2             6         0         1         0         1        0
> 3             8         0         1         0         1        0
>
>
> For instance, for each project_id I try generate a variable that for each org_id meeting cond_2 == 1 produces the highest value of var_2 for org_id that meets cond_1 == 1. In the case of project_id 1 above org_id 1 should get a score of 3, while org_id 2 and 3 should get a missing value. Some project_ids may have only one observation (consist of a single org_id) as in project_id 3 above, or may not have other org_ids represented that meet the specified condition. Then, it is of course not possible to generate a value on the variable.
>
> I have tried with the following code, but I cannot figure out how to specify cond_2 == 1, and it comes out all wrong (Stata does not even accept my code):
>
> sort project_id org_id
> gen maxwins = .
> quietly forvalues i = 1/`r(max)' {
> gen include 1 = if org_id != `i' if cond_1 == 1
> egen x = max(var_2 * include), by(project_id)
> replace maxwins = x if org_id == `i'
> drop include x
> }
>
> Is it possible to do this without loops also?

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