Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • gen variable= product(..) ?

    Hi Guys!

    Is there an easier way of creating a product variable column than making a loop?
    It seems kind of strange that it is possible to gen var= sum(...) but not gen var= product(...)

    Hope you can help me out?
    Kind regards

    Nick Ewbank

    ps: for clarification, I need the column output to look like this:
    1 = 1,01
    2= 1,01*1,02
    3= 1,01*1,02*1,03
    4= etc..

  • #2
    Code:
    clear
    set obs 20
    gen var=1.01
    replace var = var[_n-1]*1.01 if _n>1
    list

    Comment


    • #3
      Code:
      clear
      set obs 20
      gen var=1.01
      replace var = var[_n-1]*(1+ _n/100) if _n>1
      list

      Comment


      • #4
        Thank you! This helps a lot. However I am calculating holding period returns for many companies. How can I implement this code for more than 1 variable?
        This is the formula I'm using: product(t=1 to 756) (1+r_it)

        Thank you in advance!

        Comment


        • #5
          I guess one reason for the lack of a product function is that their results vanish if one term is zero (producing puzzlement to some) and can cause overflow or underflow problems all too easily (ditto). The traffic in user support might make its inclusion a dubious extra for StataCorp. Still, I said that, not them.

          In addition to Svend's tricks, which work for your examples, I note also exp(sum(log()) so long as all values are positive.
          Last edited by Nick Cox; 20 Feb 2015, 06:03.

          Comment

          Working...
          X