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

# Re: st: RE: Dropping Observations based on Observations Across Rows

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Dropping Observations based on Observations Across Rows Date Thu, 18 Oct 2012 15:30:45 +0100

```bysort date ID (Description) : drop if _N == 2 & Description[1] ==
"Purchase" & Description[2] == "Refund"

Note:

1. This is a standard use of -by:- exploiting the fact that under
-by:- _n and _N are interpreted within groups. For much more, see the
sections on [U] on -by:- and/or

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
Q1/02   SJ 2(1):86--102                                  (no commands)
explains the use of the by varlist : construct to tackle
a variety of problems with group structure, ranging from
simple calculations for each of several groups to more
advanced manipulations that use the built-in _n and _N

2. If your variable -Description- is a numeric variable with labels,
then you will need to modify the code accordingly.

On Thu, Oct 18, 2012 at 3:11 PM, Beatrice Benavidez
<beatricestata1711@gmail.com> wrote:
> Hi all,
>
> I have this example subset of a dataset:
> Date ID Description
> 1/1    1        Purchase
> 1/1    1          Refund
> 1/1    2        Purchase
> 1/1    2          Refund
> 1/1    3        Purchase
> 1/1    4        Purchase
> 2/2    1        Purchase
> 2/2    2        Purchase
> 2/2    3        Purchase
> 2/2    3          Refund
> 2/2    4        Purchase
> 2/2    4          Refund
>
> For a particular date and ID, there is a description variable where I
> have Purchase and Refund
> I would like for the one particular date and ID to then drop those
> observations completely where there are both Purchase and Refund
> counterparts in the subset dataset.
>
> I would then like my subset dataset to be reduced to:
> Date ID Description
> 1/1    3        Purchase
> 1/1    4        Purchase
> 2/2    1        Purchase
> 2/2    2        Purchase
>
> How will I go about doing this.
>
> Thank you so much!
>
> Kind regards,
>
>
> Beatrice
> *
> *   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/
```