Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Subtracting observations by group


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Subtracting observations by group
Date   Fri, 21 Jun 2013 15:53:39 +0100

You said what you wanted, but you didn't say where to put it.

Here's one way to approach it.

bysort date (group) : gen price1M3 = price[1] - price[3]

That way, the same difference will be repeated. You will want
something more limited but that's just an extra condition.
Nick
njcoxstata@gmail.com


On 21 June 2013 15:45, AGAOGLU Enes <s1236623@sms.ed.ac.uk> wrote:
> Dear Stata-listers,
>
> I have the following dataset and I would like to subtract price by groups.
>
> date group price
> 2010m1 1 10
> 2010m2 1 15
> 2010m3 1 12
> 2010m1 2 9
> 2010m2 2 21
> 2010m3 2 15
> 2010m1 3 8
> 2010m2 3 20
> 2010m3 3 16
>
> For instance, if I want to subtract group 1's prices from group 3's prices, the new variable should be the following:
>
> newvar
> 2
> -5
> -4
>
> Since observations are in the same variable(price), I couldn't figure out how to do it. I tried to create a new variable only for group 1 and group 3, but then it looked like the following. Thus, I couldn't subtracted g1 from g3.
>
> g1 g3
> 10 .
> 15 .
> 12 .
> . 8
> . 20
> . 16
>
> Thank you in advance for any help,
>
> -Enes
>
>
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
>
>
> *
> *   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index