astile :  A brief introduction

astile creates a new variable that ranks values of an existing variable on a scale of 1 to n. For example, we might be interested in making 10 firm size-based portfolios. This will involve placing the smallest 10% firms in portfolio 1, next 10% in portfolio 2, and so on. astile creates a new variable whose values ranges from 1, 2, 3, … up to n, where n is the maximum number of quantile groups specified in the nq option. For example, if we want to make 10 portfolios, values of the new variable will range from 1 to 10.

astile is faster than Stata official xtile. It’s speed efficiency matters more in larger data sets or when the quantile categories are created multiple times, e.g, we might want to create portfolios in each year or each month. Unlike Stata’s official xtile, astile is byable. astile handles group-wise calculations super efficiently. For example, the difference in time when used with bys and without bys is usually few seconds in a million observations and 1000 groups.


Example 1: Create 10 groups of firms based on thier market value
In this example, we shall use the grunfeld data set and download it within Stata from the Stata server.

webuse grunfeld
astile size10 = mvalue,  nq(10)

In the above command, we have created a new variable with the name size10. The values of size10 range from 1 to 10. These rankings are based on the values of an existing variable, mvalue. After the comma, we specify the option nq(10), that tells astile to make 10 groups. If we were to make 20 groups, then the option would be nq(20).

Example 2: Repeat ranking each year

If we want to create 5 groups of firms based on their market value in each year, we can do that using the bysort or bys option. This time we shall name the new variable as size5.

 webuse grunfeld
bys year : astile size5 = mvalue, nq(5)

Example 3: Make quantile breakpoints on a subset of the data

We can use option qc if the qunatile breakpoints need to be based on a subset of the data, and then observations in the entire data set (off course in the toused sample as created by the [if] [in] options) are to be assigned to these breakpoints. Such calculations can be frequently found in testing asset pricing models. It is standard in the field of finance to work with portfolios of stocks. Researchers form portfolios of stocks on the basis of firm-specific characteristics such size of the firms, book-to-market ratio, investments, etc. Several studies have used a sub-sample of firms to make decile breakpoints and then assign rest of the firms to these decile groups. For example, one might form decile portfolios by ranking NYSE-listed stocks on their market capitalization and define the decile breakpoints and then assign all publicly traded stocks on the NYSE, AMEX, and NASDAQ to one of these decile groups. Implementing the above with astile is very easy.

Let us use an example and see how it can be done. We shall first generate a dummy data set for our practice. We shall create 500 firms, 100 years from 1951 to 2050; three stock exchanges where these 500 firms are listed, these exchanges are NYSE, AMEX, and NASDAQ; and finally, generate market-capitalization (MarkCap) that varies for each firm in each year.


 set obs 500

 gen company=_n

 expand 100

 bys company: gen year=_n+1950

 bys company: gen xc=mod(company, 3)+1

 gen exchange = cond(xc==1, "NYSE", cond(xc==2, "AMEX", "NASDAQ"))

 gen MarkCap=uniform()*10000

So, we shall make MarkCap-based decile breakpoints from NYSE-listed firms and assign all stocks (let they be listed at NYSE, AMEX, or NASDAQ) to these decile groups.

bys year: astile size = MarkCap, qc(exchange =="NYSE") nq(10)

Your support keeps these efforts alive