Announcement

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

  • Creating a Loop to check the January Effect

    Dear Statalist,

    I have a panel dataset where stocks are uniquely identified by their permco. For each month, stocks generate returns.
    In order to obtain the 3-month returns of each stock I use the following code

    Code:
    gen return3m = .
    local N = _N
    forvalues i = 1/`N' {
        if permdiff3[`i'] == 0 {
            qui replace return3m = return[_n+3] in `i'
        }
        else {
            qui replace return3m = 0 in `i'
        }
        }
    where permdiff3 = permno3 - permno. And permno3 = permno[_n-3].

    Now I want to calculate the 3-month by excluding all observations in January. I created a dummy-variable called January (= 1)

    Code:
    gen return3m_eJ = .
    local N = _N
    forvalues i = 1/`N' {
    if permdiff3[`i'] == 0 & January[`i'] ~= 1 {
    qui replace return3m_eJ = return[_n+3] in `i'
    }
    else {
    qui replace return3m_eJ = 0 in `i'
    }
    }
    However, this code doesn't give a "return3m" of zero in October, November and December. While this is something that the code should produce right?
    As reference point; the row 36508 represent a observation in January

    Screen Shot 2017-03-27 at 12.09.25.png


    Thanks in advance,

    Huib
    Attached Files

  • #2
    I can't easily read your screenshots but there are no values visible to me for a January variable.

    We advise explicitly against screenshots in http://www.statalist.org/forums/help#stata.

    Back-tracking, I note your first block of code doesn't need a loop. This

    Code:
    gen return3m = .
    local N = _N
    forvalues i = 1/`N' {    
        if permdiff3[`i'] == 0 {        
            qui replace return3m = return[_n+3] in `i'    
        }
        else {        
            qui replace return3m = 0 in `i'    
        }    
    }
    boils down to this

    Code:
    gen return3m = cond(permdiff3 == 0, return[_n+3], 0)
    and ignoring January values may just require an if condition on that.

    Naturally you are presuming that the return 3 observations down is exactly what you want, but I have no way of understanding or checking that.

    Comment


    • #3
      Thank you for you reply. This reduces a lot of the hassle.

      gen return3m = cond(permdiff3 == 0, return[_n+3], 0) if January ~= 1
      This command does not gives the return 3 observations down if the current month is January.
      While the command

      gen return3m = cond(permdiff3 == 0, return[_n+3], 0) if January[_n+3]~= 1
      gives not the return 3 observations down if January is the month 3 observations down.

      How do I rewrite the code in such a way that these two are combined. So the command will only give a return if there are no January observations between, and including, January and January[_n+3].

      Thank you

      Comment


      • #4
        Sorry, but I don't understand your data structure to answer this. If you're lucky, someone will be familiar with the kind of data you are using and can reply. Otherwise you'll need to explain that so that someone understands. A fake dataset showing your problem may be easier to show than an extract from a real dataset. I've already cited our standard advice on presenting data examples.

        Comment


        • #5
          Okay, I am sorry if I am not clear. My file consists of stocks that are uniquely identified by the variable permno. The variable price represents the price of the stock for that month.
          I used the code
          Code:
           gen return = (price-price[_n-1]) / price[_n-1] 
          to generate the return. Now I want to calculate the return in 3 months for each observation (stock per month).
          With your help, I use
          gen return3m = cond(permdiff3 == 0, return[_n+3], 0)
          . However, I want to exclude values that are based on observations in the month January in this 3-month window/period. This new return variable will be called return3m_excludingJanuary. So, for example, the month November shouldn't produce a value for this variable. The same applies for October, November, December and January. As soon as January is part of the 3-month window, the variable "return3m_excludingJanuary" shouldn't produce a value.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(id price January permdiff3 return return3m)
          6 13.458333 1 .           .           0
          6    13.875 . .   .03095978           0
          6     13.75 . . -.009009009           0
          6 14.916667 . 0    .0848485  -.04516131
          6 16.166666 . 0   .08379882   .11351345
          6 16.145834 . 0 -.001288581  -.07766989
          6 15.416667 . 0  -.04516131   .05789476
          6 17.166666 . 0   .11351345   .23880596
          6 15.833333 . 0  -.07766989 -.004016095
          6     16.75 . 0   .05789476   .06854845
          6     20.75 . 0   .23880596   .03773579
          6 20.666666 . 0 -.004016095           0
          6 22.083334 1 0   .06854845 .0036363914
          6 22.916666 . 0   .03773579 -.018115913
          6 22.916666 . 0           0    .0756457
          6        23 . 0 .0036363914  .073756486
          6 22.583334 . 0 -.018115913  .004792332
          6 24.291666 . 0    .0756457   .07472173
          6 26.083334 . 0  .073756486   .02958582
          6 26.208334 . 0  .004792332 -.031609174
          end
          Ideally, I would like to use your
          Code:
           gen return3m = cond(permdiff3 == 0, return[_n+3], 0)
          with an
          Code:
          if
          -extension, but I am not sure how this extension must be formatted.
          Thanks again!

          Comment


          • #6
            I achieved my goal with the following command:
            Code:
            gen return3m_eJ = cond(January[_n] + January[_n+1] +  January[_n+2] + January[_n+3] == 0, return[_n+3], 0) if permdiff3 == 0
            And January is computed as:
            Code:
            gen January = 0 if month ~= 1
            I am not sure if it is the most elegant way, but it works. Thanks

            Comment

            Working...
            X