Announcement

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

  • How to write shorter

    Hi reader,

    I would like to know how I could make these formulas a bit shorter, hope you can help.

    Formula 1:

    retX and retcX are in columns, firms in rows

    gen ar2=ret2-retc2
    gen ar3=ret3-retc3
    gen ar4=ret4-retc4
    gen ar5=ret5-retc5
    gen ar6=ret6-retc6
    gen ar7=ret7-retc7
    gen ar8=ret8-retc8
    gen ar9=ret9-retc9
    gen ar10=ret10-retc10
    gen ar11=ret11-retc11
    gen ar12=ret12-retc12
    gen ar13=ret13-retc13
    gen ar14=ret14-retc14
    gen ar15=ret15-retc15
    gen ar16=ret16-retc16
    gen ar17=ret17-retc17
    gen ar18=ret18-retc18
    gen ar19=ret19-retc19
    gen ar20=ret20-retc20
    gen ar21=ret21-retc21
    gen ar22=ret22-retc22
    gen ar23=ret23-retc23
    gen ar24=ret24-retc24
    gen ar25=ret25-retc25
    gen ar26=ret26-retc26
    gen ar27=ret27-retc27
    gen ar28=ret28-retc28
    gen ar29=ret29-retc29
    gen ar30=ret30-retc30
    gen ar31=ret31-retc31
    gen ar32=ret32-retc32
    gen ar33=ret33-retc33
    gen ar34=ret34-retc34
    gen ar35=ret35-retc35
    gen ar36=ret36-retc36
    gen ar37=ret37-retc37
    gen ar38=ret38-retc38

    Formula 2:

    gen ret3year= ((1+ret2)*(1+ret3)*(1+ret4)*(1+ret5)*(1+ret6)*(1+r et7)*(1+ret8)*(1+ret9)*(1+ret10)*(1+ret11)*(1+ret1 2)*(1+ret13)*(1+ret14)*(1+ret15)*(1+ret16)*(1+ret1 7)*(1+ret18)*(1+ret19)*(1+ret20)*(1+ret21)*(1+ret2 2)*(1+ret23)*(1+ret24)*(1+ret25)*(1+ret26)*(1+ret2 7)*(1+ret28)*(1+ret29)*(1+ret30)*(1+ret31)*(1+ret3 2)*(1+ret33)*(1+ret34)*(1+ret35)*(1+ret36)*(1+ret3 7))-1

    gen retc3year= ((1+retc2)*(1+retc3)*(1+retc4)*(1+retc5)*(1+retc6) *(1+retc7)*(1+retc8)*(1+retc9)*(1+retc10)*(1+retc1 1)*(1+retc12)*(1+retc13)*(1+retc14)*(1+retc15)*(1+ retc16)*(1+retc17)*(1+retc18)*(1+retc19)*(1+retc20 )*(1+retc21)*(1+retc22)*(1+retc23)*(1+retc24)*(1+r etc25)*(1+retc26)*(1+retc27)*(1+retc28)*(1+retc29) *(1+retc30)*(1+retc31)*(1+retc32)*(1+retc33)*(1+re tc34)*(1+retc35)*(1+retc36)*(1+retc37))-1

    Formula 3:

    I need to count days per firm, since there are over 1000 different firms, what would be a smart command to generate the counted days?

    Appreciate the help


  • #2
    Not the answer you expected, but my guess is that the underlying problem here is your data set-up. My guess is that you have a wide layout (sometimes called format or structure) in which data for different times are in different variables.

    Code:
    reshape wide
    and almost all your syntax can and will be much shorter.

    Comment


    • #3
      Indeed there is, but sorry, I won't offer code in support of bad ideas.

      Comment


      • #4
        As Nick explains, your life will be easier if you have your data long.
        Code:
        *some data
        clear
        set obs 100
        gen companyID=_n
        forvalues i = 1(1)38{
        gen ret`i'=runiform()
        gen retc`i'=runiform()
        }
        
        *your formulas for a long data structure, roughly:
        reshape long ret retc, i(companyID) j(time)
        gen ar=ret-retc
        gen retplus1 = ret+1
        gen retcplus1 = retc+1
        bys companyID (time): gen runningret = retplus1*retplus1[_n-1]
        bys companyID (time): replace runningret = retplus1 if _n==1
        bys companyID (time): gen runningretc = retcplus1*retcplus1[_n-1]
        bys companyID (time): replace runningretc = retcplus1 if _n==1
        replace runningret = runningret-1
        replace runningretc = runningretc-1
        If there is a need to have the data back into wide again after, because it is sometimes easier to read, you could do:
        Code:
        reshape wide ret retc ar retplus1 retcplus1 runningret runningretc, i(companyID) j(time)
        However, for most reporting purposes, e.g., graphs, regression analyses, you'll want to keep your data long.
        I also doubt this is really easier to read with this many reporting periods and variables

        Comment


        • #5
          I would agree with Nick the majority of the Stata's commands work great with the long data format. The formulas you have posted above seem to calculate cumulative returns over a period of time. I have a blog post on this, which might be of interest to you, see https://fintechprofessor.com/2017/10...ct-of-returns/
          I this post, I use asrol (can be downloaded from SSC).
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment

          Working...
          X