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

# Re: st: egen rowmean, loops and if

 From Nick Cox <[email protected]> To [email protected] Subject Re: st: egen rowmean, loops and if Date Wed, 6 Apr 2011 00:37:43 +0100

```This would be a lot easier if you -reshape-d, even temporarily.

Otherwise, with this data structure: -egen, rowmean()- is a
non-starter here and I think you need to work at a lower level,
building up sums and counts and deriving means.

A side-detail is that -foreach- is not needed here: use -forval- instead.

Nick

On Tue, Apr 5, 2011 at 10:09 PM, Thomas Speidel <[email protected]> wrote:
> I have a wide dataset:
>  +-------------------------------------+
>  | id   occ_~1_1   occ_~2_1   occ_~3_1 |
>  |-------------------------------------|
>  |  1          4          7          . |
>  |  2        1.5          .          . |
>  |  3        2.3        3.3          . |
>  |  4        3.3        2.3        3.5 |
>  |  5        1.5          .          . |
>  |-------------------------------------|
>  |  6        1.5          .          . |
>  |  7        2.3          .          . |
>  |  8        1.5          .          . |
>  |  9        1.5        2.3        3.3 |
>  | 10        1.5        2.3        3.3 |
>  +-------------------------------------+
>
> where   occ_~1_1 = occ_met1_1
>        occ_~2_1 = occ_met2_1
>        occ_~3_1 = occ_met3_1
>
> Of course, the data is much wider (and taller), with
> occ_metj_i      j = 1 to <=3
>                i = 1 to <=8
>
> I need to create summary measures for each i that takes the mean of the
> three j's.  Two means are to be created: one that only evaluates values
>>1.5, and  one that only evaluates values <=1.5:
>
>  +-----------------------------------------------------+
>  | id   occ_~1_1   occ_~2_1   occ_~3_1 mean1_1 mean2_1 |
>  |-----------------------------------------------------|
>  |  1          4          7          .    5.50       . |
>  |  2        1.5          .          .       .    1.50 |
>  |  3        2.3        3.3          .    2.80       . |
>  |  4        3.3        2.3        3.5    3.03       . |
>  |  5        1.5          .          .       .    1.50 |
>  |-----------------------------------------------------|
>  |  6        1.5          .          .       .    1.50 |
>  |  7        2.3          .          .    2.30       . |
>  |  8        1.5          .          .       .    1.50 |
>  |  9        1.5        2.3        3.3    2.80    1.50 |
>  | 10        1.5        2.3        3.3    2.80    1.50 |
>  +-----------------------------------------------------+
>
> The main issue is that the two commands I thought of using, egen and cond,
> do not allow replace and egen, respectively.
> This loop is clearly wrong, but it was an attempt at producing what I need:
> foreach j of num 1/3    {
>        foreach i of num 1/8    {
>                egen mean1_`i' = rowmean(occ_met1_`i' occ_met2_`i'
> occ_met3_`i') if (occ_met`j'_`i'>1.5)
>                egen mean2_`i' = rowmean(occ_met1_`i' occ_met2_`i'
> occ_met3_`i') if (occ_met`j'_`i'<=1.5)
>                }
>        }
>

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