Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: Product within a variable


From   "FEIVESON, ALAN H. (AL) (JSC-SK) (NASA)" <alan.h.feiveson@nasa.gov>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: Product within a variable
Date   Thu, 12 Jun 2003 12:50:27 -0500

Or you could do it by logs if your variables takes on all positive values -
e.g.

             k          y
  1.         1          5
  2.         2          9
  3.         2          3
  4.         3          1
  5.         3          5
  6.         3          6
  7.         4          9
  8.         4          1
  9.         4         10
 10.         4          2
 11.         5          1
 12.         5          5
 13.         5         10
 14.         5          8
 15.         5         10

. gen double z=log(y)
. sort k
. egen sz=sum(z),by(k)
. gen p=exp(sz)


Then p contains the products, by k (subject to rounding errors).

. list k y p 

             k          y          p
  1.         1          5          5
  2.         2          9         27
  3.         2          3         27
  4.         3          1         30
  5.         3          5         30
  6.         3          6         30
  7.         4          9        180
  8.         4          1        180
  9.         4         10        180
 10.         4          2        180
 11.         5          1   3999.999
 12.         5          5   3999.999
 13.         5         10   3999.999
 14.         5          8   3999.999
 15.         5         10   3999.999

Al Feiveson

-----Original Message-----
From: Nick Cox [mailto:n.j.cox@durham.ac.uk]
Sent: Thursday, June 12, 2003 11:37 AM
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: Product within a variable


Mario Gamboa-Cavazos

> I am struggling to compute a constant of products within a variable.
> What I am trying to do is something very similar to what the 'egen'
> command with the option 'sum' and 'by(.)' does, except that
> I would need
> something like a multiplication option.
>
> My dataset is a panel of monthly stock returns and I want
> to compute the
> follwing:
>
> - For each company and for each year:  The product of
> (1+r_t) where r is
> the monthly stock return in month t.  More specifically, my dataset
> looks like:
>
> Company	Year		Month		Return	What I need:
> Pi^12_1(1+r_t)
> 1		1990		1		.33
> constant
> for 1990 firm 1
> 1		1990		2		.44
> constant
> for 1990 firm 1
> .		.		.		.
> constant
> for 1990 firm 1
> .		.		.		.
> constant
> for 1990 firm 1
> .		.		.		.
> constant
> for 1990 firm 1
> 1		1990		12		.55
> constant
> for 1990 firm 1
> 1		1991		1		.23
> constant
> for 1991 firm 1
> 1		1991		2		.43
> constant
> for 1991 firm 1
> .		.		.		.
> constant
> for 1991 firm 1
> .		.		.		.
> constant
> for 1991 firm 1
> .		.		.		.
> constant
> for 1991 firm 1
> 1		1991		12		.43
> constant
> for 1991 firm 1
> 2		1990		1		.87
> constant
> for 1990 firm 2
> 2		1990		2		.98
> constant
> for 1990 firm 2
> .		.		.		.
> constant
> for 1990 firm 2
> .		.		.		.
> constant
> for 1990 firm 2
> .		.		.		.
> constant
> for 1990 firm 2
> 2		1990		12		.65
> constant
> for 1990 firm 2
> .		.		.		.		.
> .		.		.		.		.
> .		.		.		.		.
>
> and so on for nearly 2,000 companies and ten years.
>
> If any of you has any good suggestions I would greatly
> appreciate it,
> especially if it can deal with missing return data.

I don't know what white magic you want for missing
values. I think you will need to spell that out.

I would try something like this:

bysort firm year (month) : gen product = return[1]
by firm year : replace product = return * product[_n-1]

But for a fuller implementation with more bells and
whistles, Phil Ryan has -egen- functions which should help.
Type

. findit egen product

Nick
n.j.cox@durham.ac.uk

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index