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,
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
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
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,
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"
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"
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)
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,
Comment