# Re: st: AW: loop question

 From Sandu Cojocaru To statalist@hsphsun2.harvard.edu Subject Re: st: AW: loop question Date Mon, 2 Nov 2009 18:43:04 -0400

```Martin,

Thanks much, this is very elegant indeed!

cheers,
sandu

On Mon, Nov 2, 2009 at 5:47 PM, Martin Weiss <martin.weiss1@gmx.de> wrote:
> <>
>
>
> *************
> clear*
>
> inp byte(group_id member_id) C
> 1 1  300
> 1 2  200
> 1 3  100
> 2 1  150
> 2 2  200
> 2 3  100
> 2 4  50
> end
>
> compress
> l, noo sepby(group_id)
>
> bys group_id (C):  /*
> */ gen diff=C[_n+1]-C[_n]
> bys group_id: gen num=_N-_n
> bys  group_id (num): /*
> */ gen outcome_var=sum(diff*num)
> sort group_id member_id
>
> drop diff num
> l, noo sepby(group_id)
> *************
>
>
> HTH
> Martin
>
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Sandu Cojocaru
> Gesendet: Montag, 2. November 2009 20:35
> An: statalist@hsphsun2.harvard.edu
> Betreff: st: loop question
>
> Dear All:
>
> I'm having trouble generating a variable that for each member i equals
> sum(Cj-Ci) over all Cj>Ci where i and j are members of the same group.
> Here's an example of the data setup - I'm trying to calculate
> `outcome_var'.
> For row 1 outcome_var=0, for row 3 = (200-100)+(300-100) = 300...and so
> on...
>
> group_id       member_id       C           outcome_var
> 1                  1                     300        0
> 1                  2                     200        100
> 1                  3                     100        300
> 2                  1                     150        50
> 2                  2                     200        0
> 2                  3                     100        150
> 2                  4                     50          300
> 3                  1 and so on...
>
> Any help would be greatly appreciated!
> sandu
> *
> *   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/
>

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