Announcement

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

  • Using foreach to loop every three years

    Hey.
    Seems like I'm having little trouble creating a forvalue loop to generate an average of returns every three years.

    I've created a foreach loop to generate an average of returns every 1 year but I can't seem to figure out how to do it every three years.

    In my data, I have a list of firms from the years 1975-2012 with their monthly returns from January to December in each year.

    My first code,
    Code:
    gen quintile=.
    replace quintile=quintileVP if yearmonth=="197501"
    replace quintile=quintileVP if yearmonth=="197801
    replace quintile=quintileVP if yearmonth=="198101
    replace quintile=quintileVP if yearmonth=="198401
    ....
    replace quintile=quintileVP if yearmonth=="201001"
    generates a variable named quintile and replaces the value in the quintile with ranking variable called quintileVP.

    quintileVP is generated every three years, in January 31st. It is a ranking of the Price-to-Earnings ratios of the firms in that month.

    In the one-year loop, I am using this code
    Code:
    levelsof year, local(yearsections)
    gen quintile=.foreach year of local yearsections{
    replace quintile = quintileVP if yearmonth == "`year'01"
    which gives me the ranking of the Price-to-Earnings ratios in January of each year.

    But I'm having trouble trying to translate the process in terms of a forvalue loop so that I can do it every three years.



    Also, with actually averaging the monthly returns, I need to do it by the ranking of the quintiles that I have generated above.
    Therefore, my code looks something like
    Code:
    bys LPERMNO: egen quintile_real1=min(quintile) if inrange(year, "1975", "1977")
    bys LPERMNO: egen quintile_real2=min(quintile) if inrange(year, "1978", 1980")
    bys LPERMNO: egen quintile_real3=min(quintile) if inrange(year, "1981", 1983")
    ...
    bys LPERMNO: egen quintile_real(N)=min(quintile) if inrange(year, "2010", 2012")
    
    bys DataD quintile_real1: egen average_threeyrs1=mean(Ret) if inrange(year,"1975", "1977")
    bys DataD quintile_real2: egen average_threeyrs2=min(quintile) if inrange(year, "1978", 1980")
    ....
    bys DataD quintile_real2: egen average_threeyrs(N)=min(quintile) if inrange(year, "2010", 2012")
    
    
    replace quintile_real1=quintile_real2 if missing(quintile_real1)
    replace quintile_real1=quintile_real3 if missing(quintile_real1)
    ...
    replace quintile_real1=quintile_real(N) if missing(quintile_real1)
    
    replace average_threeyrs1=average_threeyrs2 if missing(average_threeyrs1)
    replace average_threeyrs1=average_threeyrs3 if missing(average_threeyrs1)
    ...
    replace average_threeyrs1=average_threeyrs(N) if missing(average_threeyrs1)
    The first few lines generates a variable called quintile_real1, if the firm is in the range between 1975-1977 by their unique ID called LPERMNO and every three years until the end of the data set.

    Then the next lines generates an average called average_threeyrs1 for the firms that are in the range of 1975-1977 by their quintiles and their dates.
    This should also go on until the end of the data set.


    The final few lines are replacing the lists so that the missing values are filled in.

    If someone could help me with creating a forvalue loop so that I can automate the process it would be great!
    If there is a better way to do it other than using a forvalue loop, please help as well!!

    Thanks,




  • #2
    If someone could help me with creating a forvalue loop so that I can automate the process it would be great!
    Code:
     forval year=1978(3)2015 {
        display `year'
     }
    1978
    1981
    1984
    1987
    1990
    1993
    1996
    1999
    2002
    2005
    2008
    2011
    2014
    Best, Sergiy Radyakin

    Comment


    • #3
      Sergiy,

      Wow. Thank you so much. I could have just looked up forvalue in Stata to come up with that. May be I was thinking too hard about it..Thank you so much!!!!

      But when I tried doing

      Code:
          forval year=2009(3)2013 {
      replace quintile=quintileVP if yearmonth="`year'01"
      Stata gives me a type mismatch error.

      The variable yearmonth in my data is a string variable. Is there something wrong with my code??

      Comment


      • #4
        When I tried doing

        Code:
        forval year=2009(3)2013 {
            replace quintile=quintileVP if yearmonth== "=`year'01"
            }
        The code ran, but no changes were made..

        What I'm trying to get at is
        since the two year values in the loop are 2009 and 2012,
        I need to replace the variable quintile in 2009 and 2012 to a quintileVP variable (which ranges from 1 through 5).


        Comment


        • #5
          I got it!

          Seems that
          Code:
          forval year=2009(3)2013 {
              replace quintile=quintileVP if yearmonth== "`year'01"
              }
          works.

          I'm not sure why the previous ones didn't work though..

          Comment


          • #6
            I think because you have "=" sign in "=`year'01"

            Comment


            • #7
              Hatim is correct. The equals sign would get taken literally. Stata would be looking for that character in your string variable when no such character exists, at least at the beginning of each value.

              By the way, holding monthly dates in string variables will make some analyses more difficult and others impossible. You should set up a numeric monthly date variable using some conversion such as

              Code:
               
              gen mdate = ym( real(substr(yearmonth, 1, 4)), real(substr(yearmonth, 5,2)) ) 
              format mdate %tm
              In principle, a monthly() function exists to make this easier, but last time I looked it was buggy.

              Comment

              Working...
              X