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

# Re: st: Difference of values in the same column/variable

 From Tirthankar Chakravarty To statalist@hsphsun2.harvard.edu Subject Re: st: Difference of values in the same column/variable Date Wed, 23 Nov 2011 18:37:52 -0800

```You have a panel setup and for error-free handling in cases like this,
use Stata's -xt- machinery:

*****************************************
clear*
input group  str7 time    ewportret
1       2007m4  .0375245
1       2007m5  .0397919
1       2007m6  -.0188678
0       2007m4  .041679
0       2007m5  .0449738
0       2007m6  -.0240277
end
g time2 = monthly(time, "YM")
format time2 %tm
xtset group time2
g dewportret = D.ewportret
li
*****************************************
T

On Wed, Nov 23, 2011 at 6:23 PM, Anna Reimondos <areimondos@gmail.com> wrote:
> Hi Gokhan
> Maybe something like
>
>
> gen newvar=.
> gsort time -group
> bysort time: replace newvar=ewportret-ewportret[_n+1] if group==1 &
> group[_n+1]==0
>
> Or in a loop
>
> gen newvar=.
> forvalues n=1/6 {
> replace newvar=ewportret-ewportret[`n'] if time==time[`n'] & group==1
> }
>
>
> Anna
>
>
> On Tue, Nov 15, 2011 at 1:21 PM,  <yilmag@rpi.edu> wrote:
>> Dear All,
>>
>> How can i create a new variable that is equal to the difference in ewportret
>> (see below table) between two groups in the same month?
>> For example: the first row of the newvar below would be equal to (.0375245-
>> .041679) and the second row would be equal to(.0397919- .0449738)
>>
>>
>> group   time    ewportret  newvar
>>
>> 1       2007m4  .0375245
>> 1       2007m5  .0397919
>> 1       2007m6  -.0188678
>> 0       2007m4  .041679
>> 0       2007m5  .0449738
>> 0       2007m6  -.0240277
>>
>>
>> Thank you
>>
>> Gokhan
>>
>>
>> *
>> *   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/
>

--
Tirthankar Chakravarty
tchakravarty@ucsd.edu
tirthankar.chakravarty@gmail.com

*
*   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/
```