Bookmark and Share

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: how do you subtract the value of one group from the value of another group?


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: how do you subtract the value of one group from the value of another group?
Date   Mon, 19 Aug 2013 20:55:26 +0100

bysort year (group) : gen diff = value[_N] - value[1] if group == 2

If missings are present

egen v2 = max(value / (group == 2)) ,by(year)
egen v1 = max(value / (group == 1)), by(year)
gen diff = v2 - v1 if group == 2

Nick
[email protected]


On 19 August 2013 20:43, Ken Ning <[email protected]> wrote:
> Hi statausers
>
> how do you subtract the value of one group from the value of another group?
>
> e.g. if i want to substract the value of group 2 (value= 11 in 2001
> and 12 in 2002) from group1 (value= 7 in 2001 and 8 in 2002) year by
> year? so this needs to be 11-7 and 12-8 and listed next to the value
> of group 2
>
>
>
> id year value  (minus value)
> 1 2001   7     .
> 1 2001   7    .
> 1 2001   7   .
> 1 2002    8  .
> 1 2002    8  .
> 1 2002    8  .
>
> 2 2001   11     x (need to get)
> 2 2001   11     x
> 2 2001   11     x
> 2 2002   12     x
> 2 2002   12     x
> 2 2002   12     x
>
> Many thanks
> Ken
> *
> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index