Announcement

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

  • Sum row variables with Macro

    Hi,

    I have the following dataset:
    HTML Code:
    permno    date    ln_ret
    10000    1986m1    
    10000    1986m2    -.2972516
    10000    1986m3    .3114361
    10000    1986m4    -.1037968
    10000    1986m5    -.2518726
    10000    1986m6    -.0050378
    10000    1986m7    -.0842603
    10000    1986m8    -.9555115
    10000    1986m9    -.0588405
    10000    1986m10    -.2776318
    10000    1986m11    .0582689
    10000    1986m12    -.4737844
    10001    1986m1    
    10001    1986m2    .0202027
    10001    1986m3    .0248877
    10001    1986m4    .0098523
    10001    1986m5    -.0098523
    10001    1986m6    -.0131555
    10001    1986m7    -.0102565
    10001    1986m8    .0696799
    10001    1986m9    -.0030817
    10001    1986m10    .0384663
    10001    1986m11    .0550598
    10001    1986m12    .0148886

    I would like to add the variables ret_L1+ret_L2+...+ret_L`J'. I defined the local macro `J' in the code. How can I modify the last row? Here is what I am running

    Code:
    local J 6
    
    tsset permno date, monthly
    sort permno date
    
    forvalues i = 1(1)`J' {
    gen ret_L`i' = L`i'.ln_ret
    }
    
    local v ret_L1-ret_L`J'
    
    gen cum_ret = exp(sum(`v'))-1

  • #2
    This won't work any way.

    The function sum() gives you a cumulative sum across observations. You want a row sum across variables. Check out tsegen (SSC)

    Code:
     
    clear 
    input permno  str7 s_date    ln_ret
    10000    1986m1        .
    10000    1986m2    -.2972516
    10000    1986m3    .3114361
    10000    1986m4    -.1037968
    10000    1986m5    -.2518726
    10000    1986m6    -.0050378
    10000    1986m7    -.0842603
    10000    1986m8    -.9555115
    10000    1986m9    -.0588405
    10000    1986m10    -.2776318
    10000    1986m11    .0582689
    10000    1986m12    -.4737844
    10001    1986m1        . 
    10001    1986m2    .0202027
    10001    1986m3    .0248877
    10001    1986m4    .0098523
    10001    1986m5    -.0098523
    10001    1986m6    -.0131555
    10001    1986m7    -.0102565
    10001    1986m8    .0696799
    10001    1986m9    -.0030817
    10001    1986m10    .0384663
    10001    1986m11    .0550598
    10001    1986m12    .0148886
    end 
    gen date = monthly(s_date, "YM") 
    tsset permno date 
    
    tsegen wanted = rowtotal(L(1/6).ln_ret, 6)
    Last edited by Nick Cox; 27 Jul 2015, 02:00.

    Comment


    • #3
      Thanks for the response Nick. I would like to program in a flexible manner by using the macro `J' as described above. Would you be able to help incorporating that in your code?

      I have tried the following:
      Code:
      local J 6
      tsegen wanted = rowtotal(L(1/6).ln_ret, 6)
      Thanks again!

      Comment


      • #4
        Code:
          
        local J 6
        tsegen wanted = rowtotal(L(1/`J').ln_ret, `J')

        Comment


        • #5
          Nick,

          I was wondering how results can be slighly different using the two approach:

          Code:
          Code:
          local J 3
          gen cum_ret_test = exp(ln_ret + ret_L1 + ret_L2 + ret_L3)-1
          tsegen cum_ret0 = rowtotal(L(1/`J').ln_ret, `J')
          gen cum_ret1 = exp(ln_ret+cum_ret0)-1
          drop cum_ret0
          outcome:
          HTML Code:
          cum_ret_test    cum_ret1
          -.0480769    -.048077
          -.3591549    -.3591549
          -.7265625    -.7265625
          -.6683418    -.6683417

          Comment


          • #6
            Floating point arithmetic...

            Spell out that you want doubles if high order agreement is important to you.

            Comment

            Working...
            X