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

# Re: st: Aggregate different parts of data selectively

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: Aggregate different parts of data selectively Date Mon, 3 Feb 2014 19:33:00 +0000

```Your example is rather mysterious but you could

keep if (group == 1 & no > 2) | (group == 2 & no > 5) | (group == 3 & no > 3)

after which -collapse- would work on all specified observations.

Nick
njcoxstata@gmail.com

On 3 February 2014 19:24, Chris Yang <bhpchg@gmail.com> wrote:
> Dear All,
>
> I have a dataset of the following structure:
>
> group no var1 var2 var3
> 1       1   3     2      1
> 1       2   2     0      0
> 1       3   3     0      1
> 1       4   0     1      1
> 1       5   1     1      2
> ...
> 2       1   3     2      1
> 2       2   3     0      0
> 2       3   1     1      1
> 2       4   0     2      1
> 2       5   0     0      2
> ...
> 3       1   1     1      1
> 3       2   2     3      1
> 3       3   2     1      1
> ...
>
> I want to aggregate a subset of observations by group, e.g. aggregate
> (using the mean) all the observations with `no` > 2 in group 1,
> aggregate observations with `no` > 5 in group 2, aggregate
> observations with `no` > 3 in group 3, and so on. And carry out
> analysis using the aggregated data of different groups only.
>
> I know that --collapse-- can do the averaging job within each group,
> however, it will also replace all existing data with the aggregated
> data. In my case, it would mean that I lose the data for all the other
> groups once I collapse/aggregate for any single group.
>
> A tedious way is to collapse and save respective groups individually,
> and then merge the aggregated per-group data in the end. I wonder if
> there is a more efficient way to achieve my goal here?
>
> Your thoughts and suggestions are appreciated. Thanks.
>
> Best regards,
>
> Chris
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```