Dear Stata users,

I' m trying to build monthly value-weighted portfolio returns based 10 deciles of the asset growth rate.
At the end of June of every year t stocks are allocated into deciles based on the annual asset growth rate and the portfolios are formed from July of year t to June of year t+1. The portfolio are held for 1 year and then rebalanced.

the result of my one code is far more different to the paper I replicate, so I wonder whether there is some thing wrong with my code.

I would be grateful if someone could help to get the correct method.

here's my code:
Code:
//sort the annual asset growth rate into 10 deciles
sort year
egen d_10 = xtile(at_g), by(year_m) nq(10)

//generate month = 6 for merge the stock data in June from CRSP

gen month = 6
drop if d_10 == .
duplicates report permno year_m month

merge 1:1 permno year_m month using "2_data_output/at_stocks"
sort permno year_m month
destring gvkey, replace


//identify stocks in different deciles from July

bys permno: replace gvkey = gvkey[_n-1] if gvkey == . & gvkey[_n-1] != .
sort gvkey year_m month
bys gvkey: replace d_10 = d_10[_n-1] if _merge[_n-1] == 3 & year == year[_n-1] 
replace d_10 = . if _merge == 3
bys gvkey: replace d_10 = d_10[_n-1] if d_10[_n-1] != .  & d_10[_n] == . & _merge != 3
bys gvkey: replace d_10 = d_10[_n-1] if d_10 == . & _merge == 3

//identify year t and year t+1
gen year1 = .
replace year1 = year_m if _merge == 3

bys permno: replace year1 = year1[_n-1] if year1 == . & year1[_n-1] != .

gen year2 = year_m - year1
count if year2 > 1
count if year2 ==.

drop if year2 > 1 
drop if year2 == .

drop if year2 == 1 & month > 6
Here's part of my data:
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 gvkey double permno float(year_m month at_g d_10) byte _merge
"012305" 10027 1989  6  .24221244  8 3
""       10027 1989  7          .  . 2
""       10027 1989  8          .  . 2
""       10027 1989  9          .  . 2
""       10027 1989 10          .  . 2
""       10027 1989 11          .  . 2
""       10027 1989 12          .  . 2
""       10027 1990  1          .  . 2
""       10027 1990  2          .  . 2
""       10027 1990  3          .  . 2
""       10028 1986  1          .  . 2
""       10028 1986  2          .  . 2
""       10028 1986  3          .  . 2
""       10028 1986  4          .  . 2
""       10028 1986  5          .  . 2
""       10028 1986  6          .  . 2
""       10028 1986  7          .  . 2
""       10028 1986  8          .  . 2
""       10028 1986  9          .  . 2
""       10028 1986 10          .  . 2
""       10028 1986 11          .  . 2
""       10028 1986 12          .  . 2
""       10028 1987  1          .  . 2
""       10028 1987  2          .  . 2
""       10028 1987  3          .  . 2
""       10028 1987  4          .  . 2
""       10028 1987  5          .  . 2
""       10028 1987  6          .  . 2
""       10028 1987  7          .  . 2
""       10028 1987  8          .  . 2
""       10028 1987  9          .  . 2
""       10028 1987 10          .  . 2
""       10028 1987 11          .  . 2
""       10028 1987 12          .  . 2
""       10028 1988  1          .  . 2
""       10028 1988  2          .  . 2
""       10028 1988  3          .  . 2
""       10028 1988  4          .  . 2
""       10028 1988  5          .  . 2
""       10028 1988  6          .  . 2
""       10028 1988  7          .  . 2
""       10028 1988  8          .  . 2
""       10028 1988  9          .  . 2
""       10028 1988 10          .  . 2
""       10028 1988 11          .  . 2
""       10028 1988 12          .  . 2
""       10028 1989  1          .  . 2
""       10028 1989  2          .  . 2
""       10028 1989  3          .  . 2
""       10028 1989  4          .  . 2
""       10028 1989  5          .  . 2
"012096" 10028 1989  6   .8092424 10 3
""       10028 1989  7          .  . 2
""       10028 1989  8          .  . 2
""       10028 1989  9          .  . 2
""       10028 1989 10          .  . 2
""       10028 1989 11          .  . 2
""       10028 1989 12          .  . 2
""       10028 1990  1          .  . 2
""       10028 1990  2          .  . 2
""       10028 1990  3          .  . 2
""       10028 1990  4          .  . 2
""       10028 1990  5          .  . 2
"012096" 10028 1990  6 -.24205524  1 3
""       10028 1990  7          .  . 2
""       10028 1990  8          .  . 2
""       10028 1990  9          .  . 2
""       10028 1990 10          .  . 2
""       10028 1990 11          .  . 2
""       10028 1990 12          .  . 2
""       10028 1991  1          .  . 2
""       10028 1991  2          .  . 2
""       10028 1991  3          .  . 2
""       10028 1991  4          .  . 2
""       10028 1991  5          .  . 2
"012096" 10028 1991  6   .4659091 10 3
""       10028 1991  7          .  . 2
""       10028 1991  8          .  . 2
""       10028 1991  9          .  . 2
""       10028 1991 10          .  . 2
""       10028 1991 11          .  . 2
""       10028 1991 12          .  . 2
""       10028 1992  1          .  . 2
""       10028 1992  2          .  . 2
""       10028 1992  3          .  . 2
""       10028 1992  4          .  . 2
""       10028 1992  5          .  . 2
"012096" 10028 1992  6 -.06709436  3 3
""       10028 1992  7          .  . 2
""       10028 1992  8          .  . 2
""       10028 1992  9          .  . 2
""       10028 1992 10          .  . 2
""       10028 1992 11          .  . 2
""       10028 1992 12          .  . 2
""       10028 1993  1          .  . 2
""       10028 1993  2          .  . 2
""       10028 1993  3          .  . 2
""       10028 1993  4          .  . 2
""       10028 1993  5          .  . 2
"012096" 10028 1993  6  -.3028653  1 3
end
label values _merge _merge
label def _merge 2 "using only (2)", modify
label def _merge 3 "matched (3)", modify

and another question is that because i sort the asset growth rate before merging the annual accounting data with the monthly stock data, there's some annual data cannot be merged with the stock returns, and thus decrease the number of firms in deciles. so should I sort the deciles after merge the firms?

Thank you very much for you help in advance!