Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str15 companies byte stock_id int date double(Returns Idiosyncraticvolatility Marketcapitalisation)
"3M IN Equity"    1 16648  .22000875  .142986 8969.836
"3M IN Equity"    1 16679  .00594774 .0442478 9023.345
"3M IN Equity"    1 16709  .09604762 .1154085 9933.002
"3M IN Equity"    1 16740 -.18612806 .0927586 8246.053
"3M IN Equity"    1 16770  .04742311 .0760012 8646.527
"3M IN Equity"    1 16801  .10965069 .0958403 9648.558
"3M IN Equity"    1 16832  .07151935 .0495798 10363.89
"3M IN Equity"    1 16860  .34647638 .1465515 14655.33
"3M IN Equity"    1 16891  .11756522 .0883265 16483.66
"3M IN Equity"    1 16921 -.00785582 .1280992 16084.31
"3M IN Equity"    1 16952 -.27436253 .1219167 12430.47
"3M IN Equity"    1 16982  .03979741 .1011703 12935.15
"3M IN Equity"    1 17013 -.03140462 .0735412 12535.24
"3M IN Equity"    1 17044  .01979822 .0827738 12785.89
"3M IN Equity"    1 17074  .14347082 .1185964 14758.41
"3M IN Equity"    1 17105  .03962137  .083371 15354.89
"3M IN Equity"    1 17135  .09906806 .0820645 16953.97
"3M IN Equity"    1 17166  .00860078 .0466454 17100.42
"3M IN Equity"    1 17197  .11708011 .0865417 19224.46
"3M IN Equity"    1 17225  .02308274  .094335 19673.37
"3M IN Equity"    1 17256 -.10203222 .0797749 17765.06
"3M IN Equity"    1 17286  .07274823 .0691696 19105.61
"3M IN Equity"    1 17317  .12631571 .0807489 21677.99
"3M IN Equity"    1 17347  .07532471 .1245477 23373.96
"3M IN Equity"    1 17378 -.13217596 .0606982 20479.95
"3M IN Equity"    1 17409 -.04681604 .0867076 19543.26
"3M IN Equity"    1 17439    .044448 .0637702 20431.33
"3M IN Equity"    1 17470  .21749273 .1530644 25395.25
"3M IN Equity"    1 17500 -.13973865 .0837717 22083.34
"3M IN Equity"    1 17531  .16032942 .1357227 25923.58
"AACL IN Equity"  2 16648  .09542995 .0978906 370.1137
"AACL IN Equity"  2 16679  .19521217 .1228615 449.8987
"AACL IN Equity"  2 16709 -.10371983 .1566531 405.5737
"AACL IN Equity"  2 16740  -.1293248 .1224561  356.373
"AACL IN Equity"  2 16770  .02214113 .0605402 364.3515
"AACL IN Equity"  2 16801  .06818151 .1264464   390.06
"AACL IN Equity"  2 16832  .07864313 .1213397  421.974
"AACL IN Equity"  2 16860  .01873104 .1200355 429.9525
"AACL IN Equity"  2 16891 -.11920157 .0821024 381.6382
"AACL IN Equity"  2 16921  .54170286 .0743389 649.8045
"AACL IN Equity"  2 16952  -.2787134 .1243766   496.44
"AACL IN Equity"  2 16982 -.23889191  .101902 390.9465
"AACL IN Equity"  2 17013  .06368782 .1569841  416.655
"AACL IN Equity"  2 17044  .17520409 .1854097   496.44
"AACL IN Equity"  2 17074  .12095261 .1790345  560.268
"AACL IN Equity"  2 17105  .08489944 .1328724  609.912
"AACL IN Equity"  2 17135  .15394302  .176113 711.4162
"AACL IN Equity"  2 17166  .06336961 .1808397 757.9575
"AACL IN Equity"  2 17197  .19202095  .160783  918.414
"AACL IN Equity"  2 17225 -.12146984 .1425292 813.3637
"AACL IN Equity"  2 17256 -.05373425 .1655384 770.8117
"AACL IN Equity"  2 17286  .18910319 .1126103 931.2682
"AACL IN Equity"  2 17317 -.00764457 .1376465 924.1762
"AACL IN Equity"  2 17347 -.02919915 .0957982 897.5812
"AACL IN Equity"  2 17378 -.00992564 .1348592 888.7162
"AACL IN Equity"  2 17409  .00398209 .0735481 892.2622
"AACL IN Equity"  2 17439  .00495541  .074697 896.6947
"AACL IN Equity"  2 17470  .02441528 .1254344 918.8572
"AACL IN Equity"  2 17500 -.05554229 .1779125 869.2132
"AACL IN Equity"  2 17531  .19574458 .1349978 1057.151
"AAVAS IN Equity" 3 16648          .        .        .
"AAVAS IN Equity" 3 16679          .        .        .
"AAVAS IN Equity" 3 16709          .        .        .
"AAVAS IN Equity" 3 16740          .        .        .
"AAVAS IN Equity" 3 16770          .        .        .
"AAVAS IN Equity" 3 16801          .        .        .
"AAVAS IN Equity" 3 16832          .        .        .
"AAVAS IN Equity" 3 16860          .        .        .
"AAVAS IN Equity" 3 16891          .        .        .
"AAVAS IN Equity" 3 16921          .        .        .
"AAVAS IN Equity" 3 16952          .        .        .
"AAVAS IN Equity" 3 16982          .        .        .
"AAVAS IN Equity" 3 17013          .        .        .
"AAVAS IN Equity" 3 17044          .        .        .
"AAVAS IN Equity" 3 17074          .        .        .
"AAVAS IN Equity" 3 17105          .        .        .
"AAVAS IN Equity" 3 17135          .        .        .
"AAVAS IN Equity" 3 17166          .        .        .
"AAVAS IN Equity" 3 17197          .        .        .
"AAVAS IN Equity" 3 17225          .        .        .
"AAVAS IN Equity" 3 17256          .        .        .
"AAVAS IN Equity" 3 17286          .        .        .
"AAVAS IN Equity" 3 17317          .        .        .
"AAVAS IN Equity" 3 17347          .        .        .
"AAVAS IN Equity" 3 17378          .        .        .
"AAVAS IN Equity" 3 17409          .        .        .
"AAVAS IN Equity" 3 17439          .        .        .
"AAVAS IN Equity" 3 17470          .        .        .
"AAVAS IN Equity" 3 17500          .        .        .
"AAVAS IN Equity" 3 17531          .        .        .
"ABB IN Equity"   4 16648          . .0943647 61875.16
"ABB IN Equity"   4 16679          . .0600698 67658.14
"ABB IN Equity"   4 16709          . .0662682 72445.16
"ABB IN Equity"   4 16740          . .0447659 71160.99
"ABB IN Equity"   4 16770          . .0968998 82337.05
"ABB IN Equity"   4 16801          . .0395569 81582.65
"ABB IN Equity"   4 16832          . .0981462 107191.8
"ABB IN Equity"   4 16860          . .0617232   107925
"ABB IN Equity"   4 16891          . .0691475 124095.7
"ABB IN Equity"   4 16921          . .0517329 131137.5
end
format %tdnn/dd/CCYY date

Hello Sir,
I would like to do 5*5 bivariate sorting to form our portfolios return series. We require value weighted portfolios return series as well as equal weighted portfolios return series. I have attached my sample data above.
For value value weighted return:
First of all, at every 30th June, we independently split all companies into five size groups by taking market capitalization as a base and for breakpoint we have used 20th percentile, 40th percentile, 60th percentile and 80th percentile (we can also call it as quintile breakpoints). Further, we have divided each group into five subgroups by using 20th percentile, 40th percentile, 60th percentile and 80th percentile value of Idiosyncratic Volatility for breakpoints. By doing this 5*5 bivariate sorting on variables the Size and Idiosyncratic volatility, we get 25 portfolios. After this every year from July-June, we calculate the monthly value weighted return for all these 25 portfolios. For weights, we have used market capitalization on 30th June of every year, i.e. portfolio creation date. The same process we have repeated each year. Hence, we get the series of value-weighted monthly returns for these 25 portfolios by employing bivariate sorting. In this way, we can see the impact of Idiosyncratic volatility on stock returns while keeping size conditional.
For equal weighted return series:
Again equal weights are based on Market capitalization on 30th June of every year.


I got the code which is written below from some earlier post in Stata forum. In this code portfolios are created every month. But I would like to regenerate the portfolios at the end of every June of the year i.e. yearly as I mentioned above. Sir, what will be the change in this code. I really need help.

capture program drop one_mdate
program define one_mdate
gen cutoffs = .
_pctile size, nq(5)
forvalues i = 1/4 {
replace cutoffs = r(r`i') in `i'
}
display `"`cutoffs'"'
xtile size_quintile = size, cutpoints(cutoffs)
drop cutoffs
by size_quintile, sort: egen idiovol_quintile = xtile(idiovol), nq(5)
tab *_quintile
exit
end
// DOUBLE SORT EACH FISCAL YEAR
runby one_mdate, by(mdate) status
capture program drop one_weighted_return
program define one_weighted_return
egen numerator = total(mcap*rt)
egen denominator = total(mcap)
gen vw_mean_rt = numerator/denominator
exit
end
runby one_weighted_return, by(mdate size_quintile idiovol_quintile)
gen int portfolio_num = (size_quintile-1)*5 + idiovol_quintile
by mdate size_quintile idiovol_quintile, sort: egen ew_mean_rt = mean(rt)
keep mdate *_quintile *_mean_rt
by mdate *_quintile, sort: keep if _n == 1

Thank you
Regards