Stock Return on Stata

In the previous article, we discussed how the stock return can be calculated manually. However, in this article, we are going to learn how stock returns can be calculated through ascol command in Stata. We are using the below dataset, which contains three variables, closing price, symbol code, and date.

The command that will be used to calculate the stock returns is ascol. Ascol command doesn’t calculate the stock return directly, but it will help in converting the data frequency. The command needs to be installed before proceeding further. The below command will be used to install ascol.

ssc install ascol

To calculate the returns, first, it must tell STATA what type of data (cross-sectional, time series, or panel data) we are using. For that purpose, the below command will be used:

xtset symbol_code date

The xtset command will tell STATA that we are using Panel Data.

Convert Daily Prices Data into Weekly Stock Return

We can either have prices data or returns data. Let’s first understand how to convert daily prices data into weekly return. For this we will use the following command:

ascol closing, toweek prices

First, we will write the command and then the name of the variable that we want to use. After this, we will add some options that will depend on what data we have or what data we want to use. In our case, we want the data to be weekly, so for that, we are using the toweek option. This will convert the data into weekly data. Lastly, we will use the price option because we have price data. The above command will only convert data into weekly format, retaining the last date of the week. However, it will not calculate the return. Now, we will use the below command to calculate the stock return.

bys symbol_code (week_id): gen stock_return = ln (week_closing / week_closing[_n-1])

The above command will provide the weekly stock return data.

Convert Daily Return Data into Weekly Stock Return

In cases where we have return data instead of price data, we will use the return option in place of prices. However, before that, it is important to understand that there are two ways to compute the return in finance:

1. Simple Return

return = (current value – previous value) / previous value

2. Log Return

return = ln (current value / previous value)

For example, if we had the data of log return, then the below command will be used to calculate the weekly return.

ascol closing, toweek returns (log)

And if we had the data of simple return, then we will use simple option in place of the log as shown below:

ascol closing, toweek returns (simple)

We can also calculate the monthly, quarterly, and yearly returns by sampling and modifying the command as shown below:

ascol closing, tomonth returns (log)
ascol closing, toquarter returns (log)
ascol closing, toyear returns (log)

As discussed above, the xtset command in Stata is utilized to specify panel data, which involves grouping observations into panels or clusters. The command necessitates a distinct identifier variable. The xtset does not support duplicate data in the dataset, which means multiple observations with the same unique identifier. Therefore, in that case, we need to tell Stata about the time variable in our dataset. To do so, we will add the timevar at the end of the ascol command, as shown below:

ascol closing, toweek prices timevar(varname)

In our case, the time variable is “date”.

ascol closing, toweek prices timevar(date)

Keep option

You will notice that when you run the below command, STATA will drop all the variables that are not required to calculate the stock return.

ascol closing, toweek prices

With the help of the keep option, we can store the data of the variable we don’t want to drop.

ascol closing, toweek prices keep(all)

By writing “all” within the parentheses, all the variable’s data can be stored. You can also store the data for specific variables by specifying the names of the variables within the parentheses you want to keep.

ascol closing, toweek prices keep(varname)

Generate Option

We can also name the variable to generate after running the ascol command (week_closing) of our own choice with the help of the the generate option. Within the parentheses, we will write the name we want to give to the week_closing.  

ascol closing, toweek prices generate(abc)
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x