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

# Re: st: RE: Insert new observation by group

 From Fernando Luco To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Insert new observation by group Date Wed, 2 Nov 2011 16:32:43 -0500

```Thanks Nick. I modified the example before sending it and I didn't
change the sums in the text, sorry about that.
And thanks about the idea about using expand, I didn't know it was possible.

Thanks,

Fernando

On Wed, Nov 2, 2011 at 4:25 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> I don't understand all your requirements. I have 1 + 2 + 2 = 5 (not 6) for group 1 and similarly get a different sum for group 2.
>
> Also, you don't explain where -ingap- comes from.
>
> The bit you may be missing most is -expand-. This may give you a start.
>
> bysort group : gen order = _n
> by group: gen last = _n == _N
> expand 2 if last
> bysort group (order) : replace y = <whatever> if _n == _N
> by group : replace x = <something else> if _n == _N
>
> Nick
> n.j.cox@durham.ac.uk
>
> Fernando Luco
>
> I'm trying to create a new observation for each group in my data, but
> I haven't been able to do so. I tried using -ingap- but didn't work
> because
> it inserts the new observation at the top of the group or after the
> first observation.
> My data looks as follows:
>
> group y  x
> 1       1       10
> 1       2       10
> 1       2       10
> 2       3       8
> 2       1       8
>
> What I want is to insert a new observation at the end of each group,
> and this observation must be the difference between "x" (in my case
> this is 10 for group one and 8 for group two) and the sum of "y"
> (6 for group one and 3 for group two). So, the idea is that the final
> dataset should look as follows
>
> group y x
> 1       1       10
> 1       2       10
> 1       2       10
> 1      5       10
> 2       3       8
> 2       1       8
> 2      4       8
>
> Any ideas? Thanks,
>
> Fernando
> *
> *   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/
```