Dear all,
I am struggling to replicate the FF-25 portfolios with a variant, I should employ a dependent sort instead of a independent sort.
My dataset is the following one:
permno date primexch ret year month datem BM id MarketCap
10001 30-May-86 Q -0.00980 1986 5 316 . 2 .
10001 30-Jun-86 Q -0.01307 1986 6 317 . 2 1.797265
10001 31-Jul-86 Q -0.01020 1986 7 318 . 2 1.797265
10001 29-Aug-86 Q 0.07216 1986 8 319 . 2 1.797265
10001 30-Sep-86 Q -0.00308 1986 9 320 . 2 1.797265
10001 31-Oct-86 Q 0.03922 1986 10 321 . 2 1.797265
10001 28-Nov-86 Q 0.05660 1986 11 322 . 2 1.797265
10001 31-Dec-86 Q 0.01500 1986 12 323 . 2 1.797265
10001 30-Jan-87 Q -0.03571 1987 1 324 . 2 1.797265
10001 27-Feb-87 Q -0.07407 1987 2 325 . 2 1.797265
10001 31-Mar-87 Q 0.03680 1987 3 326 . 2 1.797265
10001 30-Apr-87 Q -0.03922 1987 4 327 . 2 1.797265
10001 29-May-87 Q -0.07143 1987 5 328 . 2 1.797265
10001 30-Jun-87 Q 0.05143 1987 6 329 1.0144155 2 1.761665
10001 31-Jul-87 Q 0.02128 1987 7 330 1.0144155 2 1.761665
10001 31-Aug-87 Q 0.08333 1987 8 331 1.0144155 2 1.761665
10001 30-Sep-87 Q -0.02231 1987 9 332 1.0144155 2 1.761665
10001 30-Oct-87 Q 0.02000 1987 10 333 1.0144155 2 1.761665
10001 30-Nov-87 Q -0.02941 1987 11 334 1.0144155 2 1.761665
10001 31-Dec-87 Q -0.03354 1987 12 335 1.0144155 2 1.761665
10001 29-Jan-88 Q 0.06383 1988 1 336 1.0144155 2 1.761665
10001 29-Feb-88 Q 0.08000 1988 2 337 1.0144155 2 1.761665
10001 31-Mar-88 Q -0.07630 1988 3 338 1.0144155 2 1.761665
10001 29-Apr-88 Q 0.03061 1988 4 339 1.0144155 2 1.761665
10001 31-May-88 Q 0.01980 1988 5 340 1.0144155 2 1.761665
10001 30-Jun-88 Q -0.01204 1988 6 341 1.2076184 2 1.824549
10001 29-Jul-88 Q 0.03000 1988 7 342 1.2076184 2 1.824549
10001 31-Aug-88 Q 0.02913 1988 8 343 1.2076184 2 1.824549
10001 30-Sep-88 Q -0.021132076 1988 9 344 1.2076184 2 1.824549
10001 31-Oct-88 Q 0.039215688 1988 10 345 1.2076184 2 1.824549
10001 30-Nov-88 Q 0 1988 11 346 1.2076184 2 1.824549
where permo identifies the company, primexch identifies the stock exhanges ( Q=Nasdaq, N=Nyse, A=Amex), ret indicates the returns, BM (book-to-market value) is calculated at the end of the year t and it results to be publicly available in June of the year t+1 until May of the year t+2, finally MarketCap indicates the size of each company and it is calculated in June of year t and remains constant until May of year t+1.
I should sort stocks into 5 quintiles in accordance with their BM, and secondly I should double sort within each quintile according to companies’ Market Cap. The quintiles breakpoints should be calculated using only NYSE stocks (“N”).
Therefore I should obtain 25 portfolios, which are firstly sorted on BM and secondly on MarketCap.
Finally I should calculated the value-weighted monthly returns on these 25 portfolios from July of year t to June of year t+1.

Ps: I tried to write a code for calculate the value-weighted monthly returns on 10 deciles sorted on MarketCap for another type of calculation I had to do..Maybe it could be helpful
forvalues i = 1(1)10 {
egen num_return_dec_`i' = total(MarketCap * ret * !missing(MarketCap, ret)) if deciles_MarketCap==`i', by (datem)
egen den_return_dec_`i' = total(MarketCap * !missing(MarketCap, ret)) if deciles_MarketCap==`i', by (datem)
gen vw_return_dec`i' = num_return_dec_`i'/den_return_dec_`i' if deciles_MarketCap==`i'
}
Any help would be really appreciated as it is one week that I have been trying to solve this problem.
Best regards,
Antonio