![]() So, the command keep if n=N will only keep the observations where both the variables have the same values i.e., only the last observations for each week. For example, in the 6th week we have a total 5 number of observations whereas in the 7 th week there are 4 total observations. From the below picture you can see that “n” holds the sequence of observations within that specific week, whereas “N” has the total number of observations in that week. The above commands will generate two variables i.e., small “n” and capital “N”. To further simplify the idea of “_n” and “_N”, let’s use following commands, you don’t have to use them for stock return calculation, it is just to illustrate a point. So keep if _n=_N will only keep the last observation for each week. _n stands for sequence of the observation whereas, _N stands for the total number of observations. The bys symbol_code wofd prefix will repeat the command after the colon for each symbol and week. The above code will only keep the last observation for each week and each symbol_code. ![]() Next, we are only going to retain only the closing prices that are last day of the week. So “2011w2” means the second week of the month and so on. The wofd variable generated using the above code is not meaningful to the human eye, so we will format it using the following code: format wofd %tw ![]() The above code will generate a variable wofd, although you can use any name.Īfter the equality sign we have used wofd function, this function will generate the week of the year from the dates stored in the date variable. Therefore, to generate weekly stock returns, we will first generate a new variable that would contain the weeks of the year. The weekly returns of stock equities are commonly used in empirical research to avoid the non-synchronicity of daily data. Before executing the generate command for each firm, Stata will first sort each firm’s data based on the date variable because we have given the date variable within parenthesis. As the same dates exist for each company, therefore, before executing the command it will first sort the data on “date” variable. So, the bys (bysort) prefix will repeat whatever command is written after colen : for each firm. The “symbol_code” variable contains a unique code or symbol for each firm. In case of panel data, the correct code should be following: bys symbol_code (date): gen stock_return = ln(closing / closing) Firstly, because in the previous code we have not sorted the time variable, and secondly because we are not explicitly telling Stata that we have panels data. Now the above code works fine if we only have one time series in our data and it is correctly sorted, however, if we have panel data then the above code will not work correctly. ln function is used for natural log, whereas _n-1 is used for the previous closing price. ![]() The above code will generate a variable “ri” and it will store stock return data. Following is the code to calculate log return: gen ri=ln(closing/closing) Log return is calculated by taking the natural log of current and previous price differences. Mostly in research, we use log return as opposed to simple return. There are two methods of calculating stock return i.e., simple return and log return. ![]() Download Do and Data files Daily Stock return ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |