Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Insert new observation by group
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Insert new observation by group
Date
Thu, 3 Nov 2011 08:04:12 +0000
However, to echo a comment in another thread, what you want is also a
bad idea. It's a spreadsheet idea imported into a statistical software
context. Group characteristics are best stored as group
characteristics. If you do this, then you have to remember to exclude
the observations with summaries from every analysis thereafter. It
seems unlikely that you would really prefer to do that.
Nick
On Wed, Nov 2, 2011 at 9:30 PM, Fernando Luco <[email protected]> wrote:
> Thanks for quick response Austin. It was just what I was trying to do.
>
> On Wed, Nov 2, 2011 at 4:22 PM, Austin Nichols <[email protected]> wrote:
>> Fernando Luco <[email protected]> :
>>
>> clear
>> input group y x
>> 1 1 10
>> 1 2 10
>> 1 2 10
>> 2 3 8
>> 2 1 8
>> end
>> g obs=_n
>> bys group x: g e=1+(_N==_n)
>> expand e
>> bys group x (obs): replace y=. if _n==_N
>> by group x: g sumy=sum(y)
>> replace y=x-sumy if mi(y)
>> list, sepby(group)
>>
>>
>> On Wed, Nov 2, 2011 at 5:13 PM, Fernando Luco <[email protected]> wrote:
>>> Dear all,
>>>
>>> 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
>>>
*
* 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/