Code:
* Example generated by -dataex-. For more info, type help dataex clear input int stock_id str10 month str56 stock float(pr mcap bmr) 1 "01-03-2000" "3M India Ltd." . . .09124088 1 "30-05-2000" "3M India Ltd." 479 . . 1 "31-05-2000" "3M India Ltd." 500 . . 1 "01-06-2000" "3M India Ltd." 470.7 . . 1 "02-06-2000" "3M India Ltd." . . . 1 "05-06-2000" "3M India Ltd." 504.9 . . 1 "06-06-2000" "3M India Ltd." 513.95 . . 1 "07-06-2000" "3M India Ltd." 525 . . 1 "08-06-2000" "3M India Ltd." 555.75 . . 1 "09-06-2000" "3M India Ltd." 600.2 . . 1 "12-06-2000" "3M India Ltd." 648.2 . . 1 "13-06-2000" "3M India Ltd." 700 . . 1 "14-06-2000" "3M India Ltd." 650 . . 1 "15-06-2000" "3M India Ltd." 616.35 . . 1 "16-06-2000" "3M India Ltd." 607.9 . . 1 "19-06-2000" "3M India Ltd." 639 . . 1 "20-06-2000" "3M India Ltd." 657.85 . . 1 "21-06-2000" "3M India Ltd." 616.5 . . 1 "22-06-2000" "3M India Ltd." 630 . . 1 "23-06-2000" "3M India Ltd." 604 . . 1 "26-06-2000" "3M India Ltd." 600 . . 1 "27-06-2000" "3M India Ltd." 639.4 . . 1 "28-06-2000" "3M India Ltd." 684 . . 1 "29-06-2000" "3M India Ltd." 630.1 . . 1 "30-06-2000" "3M India Ltd." 620.05 698.49 . 1 "03-07-2000" "3M India Ltd." 633.35 . . 1 "04-07-2000" "3M India Ltd." 648.75 . . 1 "05-07-2000" "3M India Ltd." 640 . . 1 "06-07-2000" "3M India Ltd." 628 . . 1 "07-07-2000" "3M India Ltd." 615 . . 1 "10-07-2000" "3M India Ltd." 600 . . 1 "11-07-2000" "3M India Ltd." 610.65 . . 1 "12-07-2000" "3M India Ltd." 610 . . 1 "13-07-2000" "3M India Ltd." 603.5 . . 1 "14-07-2000" "3M India Ltd." 605 . . 1 "17-07-2000" "3M India Ltd." 617.8 . . 1 "18-07-2000" "3M India Ltd." 619 . . 1 "19-07-2000" "3M India Ltd." 595.1 . . 1 "20-07-2000" "3M India Ltd." 587.5 . . 1 "21-07-2000" "3M India Ltd." 586.25 . . 1 "24-07-2000" "3M India Ltd." 587 . . 1 "25-07-2000" "3M India Ltd." 605 . . 1 "26-07-2000" "3M India Ltd." 580 . . 1 "27-07-2000" "3M India Ltd." 609.75 . . 1 "28-07-2000" "3M India Ltd." 634 . . 1 "31-07-2000" "3M India Ltd." 664 . . 1 "01-08-2000" "3M India Ltd." 714.25 . . 1 "02-08-2000" "3M India Ltd." 677.35 . . 1 "03-08-2000" "3M India Ltd." 670 . . 1 "04-08-2000" "3M India Ltd." 665.2 . . 1 "07-08-2000" "3M India Ltd." 647.5 . . 1 "08-08-2000" "3M India Ltd." 670.6 . . 1 "09-08-2000" "3M India Ltd." 675 . . 1 "10-08-2000" "3M India Ltd." 651.55 . . 1 "11-08-2000" "3M India Ltd." 651.75 . . 1 "14-08-2000" "3M India Ltd." 623 . . 1 "16-08-2000" "3M India Ltd." 651 . . 1 "17-08-2000" "3M India Ltd." 640 . . 1 "18-08-2000" "3M India Ltd." 674 . . 1 "21-08-2000" "3M India Ltd." 685 . . 1 "22-08-2000" "3M India Ltd." 660 . . 1 "23-08-2000" "3M India Ltd." 660 . . 1 "24-08-2000" "3M India Ltd." 653 . . 1 "25-08-2000" "3M India Ltd." 622 . . 1 "28-08-2000" "3M India Ltd." 665.4 . . 1 "29-08-2000" "3M India Ltd." 651.25 . . 1 "30-08-2000" "3M India Ltd." 655 . . 1 "31-08-2000" "3M India Ltd." 639.4 . . 1 "04-09-2000" "3M India Ltd." 649.65 . . 1 "05-09-2000" "3M India Ltd." 650.05 . . 1 "06-09-2000" "3M India Ltd." 650 . . 1 "07-09-2000" "3M India Ltd." 650 . . 1 "08-09-2000" "3M India Ltd." 650.75 . . 1 "11-09-2000" "3M India Ltd." 646 . . 1 "12-09-2000" "3M India Ltd." 655 . . 1 "13-09-2000" "3M India Ltd." 630 . . 1 "14-09-2000" "3M India Ltd." 635 . . 1 "15-09-2000" "3M India Ltd." 627.45 . . 1 "18-09-2000" "3M India Ltd." 635 . . 1 "19-09-2000" "3M India Ltd." 645.5 . . 1 "20-09-2000" "3M India Ltd." 645 . . 1 "21-09-2000" "3M India Ltd." 631 . . 1 "22-09-2000" "3M India Ltd." 607 . . 1 "25-09-2000" "3M India Ltd." 630 . . 1 "26-09-2000" "3M India Ltd." 615 . . 1 "27-09-2000" "3M India Ltd." 625.4 . . 1 "28-09-2000" "3M India Ltd." 605.3 . . 1 "29-09-2000" "3M India Ltd." 602 . . 1 "03-10-2000" "3M India Ltd." 610 . . 1 "04-10-2000" "3M India Ltd." . . . 1 "05-10-2000" "3M India Ltd." 610 . . 1 "06-10-2000" "3M India Ltd." 591.45 . . 1 "09-10-2000" "3M India Ltd." 583 . . 1 "10-10-2000" "3M India Ltd." 549.6 . . 1 "11-10-2000" "3M India Ltd." 550 . . 1 "12-10-2000" "3M India Ltd." 553.95 . . 1 "13-10-2000" "3M India Ltd." 540.25 . . 1 "16-10-2000" "3M India Ltd." 557.85 . . 1 "17-10-2000" "3M India Ltd." 536 . . 1 "18-10-2000" "3M India Ltd." 509.6 . . 1 "19-10-2000" "3M India Ltd." 525 . . 1 "20-10-2000" "3M India Ltd." 548.2 . . 1 "23-10-2000" "3M India Ltd." 510 . . 1 "24-10-2000" "3M India Ltd." 526 . . 1 "25-10-2000" "3M India Ltd." 561.85 . . 1 "26-10-2000" "3M India Ltd." . . . 1 "27-10-2000" "3M India Ltd." 545.1 . . 1 "30-10-2000" "3M India Ltd." 535 . . 1 "31-10-2000" "3M India Ltd." 525.2 . . 1 "01-11-2000" "3M India Ltd." 567.15 . . 1 "02-11-2000" "3M India Ltd." 578.65 . . 1 "03-11-2000" "3M India Ltd." 573.75 . . 1 "06-11-2000" "3M India Ltd." 607.05 . . 1 "07-11-2000" "3M India Ltd." 599.5 . . 1 "08-11-2000" "3M India Ltd." 605 . . 1 "09-11-2000" "3M India Ltd." 594 . . 1 "10-11-2000" "3M India Ltd." 571.45 . . 1 "13-11-2000" "3M India Ltd." 591 . . 1 "14-11-2000" "3M India Ltd." 595.15 . . 1 "15-11-2000" "3M India Ltd." 590 . . 1 "16-11-2000" "3M India Ltd." 593.85 . . 1 "17-11-2000" "3M India Ltd." 595 . . 1 "20-11-2000" "3M India Ltd." 601 . . 1 "21-11-2000" "3M India Ltd." 603 . . 1 "22-11-2000" "3M India Ltd." 593.15 . . 1 "23-11-2000" "3M India Ltd." 593 . . 1 "24-11-2000" "3M India Ltd." 606.25 . . 1 "27-11-2000" "3M India Ltd." 610 . . 1 "28-11-2000" "3M India Ltd." 613.05 . . 1 "29-11-2000" "3M India Ltd." 606.05 . . 1 "30-11-2000" "3M India Ltd." 602.15 . . 1 "01-12-2000" "3M India Ltd." 606.65 . . 1 "04-12-2000" "3M India Ltd." 609 . . 1 "05-12-2000" "3M India Ltd." 623.4 . . 1 "06-12-2000" "3M India Ltd." 620 . . 1 "07-12-2000" "3M India Ltd." 620 . . 1 "08-12-2000" "3M India Ltd." 633.5 . . 1 "11-12-2000" "3M India Ltd." 619.5 . . 1 "12-12-2000" "3M India Ltd." 620.3 . . 1 "13-12-2000" "3M India Ltd." 626.5 . . 1 "14-12-2000" "3M India Ltd." 627.55 . . 1 "15-12-2000" "3M India Ltd." 628.35 . . 1 "18-12-2000" "3M India Ltd." 620 . . 1 "19-12-2000" "3M India Ltd." 645 . . 1 "20-12-2000" "3M India Ltd." 635 . . 1 "21-12-2000" "3M India Ltd." 634.45 . . 1 "22-12-2000" "3M India Ltd." 630 . . 1 "26-12-2000" "3M India Ltd." 632 . . 1 "27-12-2000" "3M India Ltd." 644 . . 1 "28-12-2000" "3M India Ltd." 640.15 . . 1 "29-12-2000" "3M India Ltd." 659.9 . . 1 "01-01-2001" "3M India Ltd." 657 . . 1 "02-01-2001" "3M India Ltd." 659.9 . . 1 "03-01-2001" "3M India Ltd." 657.75 . . 1 "04-01-2001" "3M India Ltd." 660 . . 1 "05-01-2001" "3M India Ltd." 650.05 . . 1 "08-01-2001" "3M India Ltd." 650 . . 1 "09-01-2001" "3M India Ltd." 650 . . 1 "10-01-2001" "3M India Ltd." 640.55 . . 1 "11-01-2001" "3M India Ltd." 650 . . 1 "12-01-2001" "3M India Ltd." 650 . . 1 "15-01-2001" "3M India Ltd." 645 . . 1 "16-01-2001" "3M India Ltd." 636.25 . . 1 "17-01-2001" "3M India Ltd." 629 . . 1 "18-01-2001" "3M India Ltd." 646.75 . . 1 "19-01-2001" "3M India Ltd." 640 . . 1 "22-01-2001" "3M India Ltd." 636 . . 1 "23-01-2001" "3M India Ltd." 615.1 . . 1 "24-01-2001" "3M India Ltd." . . . 1 "25-01-2001" "3M India Ltd." 621.2 . . 1 "29-01-2001" "3M India Ltd." 620 . . 1 "30-01-2001" "3M India Ltd." 623.1 . . 1 "31-01-2001" "3M India Ltd." 627 . . 1 "01-02-2001" "3M India Ltd." 615 . . 1 "02-02-2001" "3M India Ltd." 615.5 . . 1 "05-02-2001" "3M India Ltd." 621 . . 1 "06-02-2001" "3M India Ltd." 626 . . 1 "07-02-2001" "3M India Ltd." 616.05 . . 1 "08-02-2001" "3M India Ltd." 621.5 . . 1 "09-02-2001" "3M India Ltd." 620 . . 1 "12-02-2001" "3M India Ltd." 629.85 . . 1 "13-02-2001" "3M India Ltd." 630 . . 1 "14-02-2001" "3M India Ltd." 646.9 . . 1 "15-02-2001" "3M India Ltd." 624.9 . . 1 "16-02-2001" "3M India Ltd." 608.1 . . 1 "19-02-2001" "3M India Ltd." 620 . . 1 "20-02-2001" "3M India Ltd." 620 . . 1 "21-02-2001" "3M India Ltd." 610 . . 1 "22-02-2001" "3M India Ltd." 610 . . 1 "23-02-2001" "3M India Ltd." 607 . . 1 "26-02-2001" "3M India Ltd." 595 . . 1 "27-02-2001" "3M India Ltd." 607.1 . . 1 "28-02-2001" "3M India Ltd." 610 . . 1 "01-03-2001" "3M India Ltd." 615.5 . . 1 "02-03-2001" "3M India Ltd." 595.25 . . 1 "05-03-2001" "3M India Ltd." 580 . . 1 "07-03-2001" "3M India Ltd." 598 . . 1 "08-03-2001" "3M India Ltd." 614.8 . . 1 "09-03-2001" "3M India Ltd." 569.15 . . 1 "12-03-2001" "3M India Ltd." 567 . . 1 "13-03-2001" "3M India Ltd." 563.9 . . 1 "14-03-2001" "3M India Ltd." 569.5 . . 1 "15-03-2001" "3M India Ltd." 569.95 . . 1 "16-03-2001" "3M India Ltd." 540 . . 1 "19-03-2001" "3M India Ltd." 531.65 . . 1 "20-03-2001" "3M India Ltd." 512 . . 1 "21-03-2001" "3M India Ltd." 492 . . 1 "22-03-2001" "3M India Ltd." 492 . . 1 "23-03-2001" "3M India Ltd." 480.05 . . 1 "26-03-2001" "3M India Ltd." 456 . . 1 "27-03-2001" "3M India Ltd." 438.65 . . 1 "28-03-2001" "3M India Ltd." 430.35 . . 1 "29-03-2001" "3M India Ltd." 452 . . 1 "30-03-2001" "3M India Ltd." 438.8 . . 1 "02-04-2001" "3M India Ltd." 450 . . 1 "03-04-2001" "3M India Ltd." 435 . . 1 "04-04-2001" "3M India Ltd." 440.45 . . 1 "06-04-2001" "3M India Ltd." 430.05 . . 1 "09-04-2001" "3M India Ltd." 434.35 . . 1 "10-04-2001" "3M India Ltd." 430 . . 1 "11-04-2001" "3M India Ltd." 434.9 . . 1 "12-04-2001" "3M India Ltd." 418.95 . . 1 "16-04-2001" "3M India Ltd." 425 . . 1 "17-04-2001" "3M India Ltd." 430 . . 1 "18-04-2001" "3M India Ltd." 436 . . 1 "19-04-2001" "3M India Ltd." 436.7 . . 1 "20-04-2001" "3M India Ltd." 430 . . 1 "23-04-2001" "3M India Ltd." 439 . . 1 "24-04-2001" "3M India Ltd." 424 . . 1 "25-04-2001" "3M India Ltd." 434.5 . . 1 "26-04-2001" "3M India Ltd." 435 . . 1 "27-04-2001" "3M India Ltd." 422 . . 1 "30-04-2001" "3M India Ltd." 424.5 . . 1 "02-05-2001" "3M India Ltd." 415 . . 1 "03-05-2001" "3M India Ltd." 416 . . 1 "04-05-2001" "3M India Ltd." 404.4 . . 1 "07-05-2001" "3M India Ltd." 409.45 . . 1 "08-05-2001" "3M India Ltd." 400 . . 1 "09-05-2001" "3M India Ltd." 394 . . 1 "10-05-2001" "3M India Ltd." 384.85 . . 1 "11-05-2001" "3M India Ltd." 383.5 . . 1 "14-05-2001" "3M India Ltd." 380.1 . . 1 "15-05-2001" "3M India Ltd." 379.9 . . 1 "16-05-2001" "3M India Ltd." 380 . . 1 "17-05-2001" "3M India Ltd." 400 . . 1 "18-05-2001" "3M India Ltd." 417.85 . . 1 "21-05-2001" "3M India Ltd." 444.35 . . 1 "22-05-2001" "3M India Ltd." 429.95 . . 1 "23-05-2001" "3M India Ltd." 439 . . 1 "24-05-2001" "3M India Ltd." 432 . . 1 "25-05-2001" "3M India Ltd." 428.9 . . 1 "28-05-2001" "3M India Ltd." 439 . . 1 "29-05-2001" "3M India Ltd." 430.05 . . 1 "30-05-2001" "3M India Ltd." 428 . . 1 "31-05-2001" "3M India Ltd." 424.9 . . 1 "01-06-2001" "3M India Ltd." 422 . . 1 "04-06-2001" "3M India Ltd." 420 . . 1 "05-06-2001" "3M India Ltd." 415 . . 1 "06-06-2001" "3M India Ltd." 414 . . 1 "07-06-2001" "3M India Ltd." 399.35 . . 1 "08-06-2001" "3M India Ltd." 395 . . 1 "11-06-2001" "3M India Ltd." 396.8 . . 1 "12-06-2001" "3M India Ltd." 370 . . 1 "13-06-2001" "3M India Ltd." 393.1 . . 1 "14-06-2001" "3M India Ltd." 385.1 . . 1 "15-06-2001" "3M India Ltd." 385.2 . . 1 "18-06-2001" "3M India Ltd." 380 . . 1 "19-06-2001" "3M India Ltd." 371.75 . . 1 "20-06-2001" "3M India Ltd." 383.75 . . 1 "21-06-2001" "3M India Ltd." 386.75 . . 1 "22-06-2001" "3M India Ltd." 374.25 . . 1 "25-06-2001" "3M India Ltd." . . . 1 "26-06-2001" "3M India Ltd." 373 . . 1 "27-06-2001" "3M India Ltd." 373 . . 1 "28-06-2001" "3M India Ltd." 380 . . 1 "29-06-2001" "3M India Ltd." 380.1 . . 2 "01-03-2000" "A B B India Ltd." . . .4065041 2 "30-05-2000" "A B B India Ltd." 36.46 . . 2 "31-05-2000" "A B B India Ltd." 37.99 . . 2 "01-06-2000" "A B B India Ltd." 36.17 . . 2 "02-06-2000" "A B B India Ltd." 36.13 . . 2 "05-06-2000" "A B B India Ltd." 36.51 . . 2 "06-06-2000" "A B B India Ltd." 36.99 . . 2 "07-06-2000" "A B B India Ltd." 38.71 . . 2 "08-06-2000" "A B B India Ltd." 41.09 . . 2 "09-06-2000" "A B B India Ltd." 41.11 . . 2 "12-06-2000" "A B B India Ltd." 40.02 . . 2 "13-06-2000" "A B B India Ltd." 43.48 . . 2 "14-06-2000" "A B B India Ltd." 43.37 . . 2 "15-06-2000" "A B B India Ltd." 44.35 . . 2 "16-06-2000" "A B B India Ltd." 45.82 . . 2 "19-06-2000" "A B B India Ltd." 45.36 . . 2 "20-06-2000" "A B B India Ltd." 45.02 . . 2 "21-06-2000" "A B B India Ltd." 43.44 . . 2 "22-06-2000" "A B B India Ltd." 43.75 . . 2 "23-06-2000" "A B B India Ltd." 42.04 . . 2 "26-06-2000" "A B B India Ltd." 42.34 . . 2 "27-06-2000" "A B B India Ltd." 42.25 . . 2 "28-06-2000" "A B B India Ltd." 42.22 . . 2 "29-06-2000" "A B B India Ltd." 42.25 . . 2 "30-06-2000" "A B B India Ltd." 41.2 853.22 . 2 "03-07-2000" "A B B India Ltd." 41.75 . . 2 "04-07-2000" "A B B India Ltd." 41.83 . . 2 "05-07-2000" "A B B India Ltd." 42.03 . . 2 "06-07-2000" "A B B India Ltd." 41.86 . . 2 "07-07-2000" "A B B India Ltd." 42.29 . . 2 "10-07-2000" "A B B India Ltd." 43.41 . . 2 "11-07-2000" "A B B India Ltd." 44.7 . . 2 "12-07-2000" "A B B India Ltd." 47.44 . . 2 "13-07-2000" "A B B India Ltd." 45.93 . . 2 "14-07-2000" "A B B India Ltd." 46.22 . . 2 "17-07-2000" "A B B India Ltd." 45.89 . . 2 "18-07-2000" "A B B India Ltd." 44.47 . . 2 "19-07-2000" "A B B India Ltd." 41.05 . . 2 "20-07-2000" "A B B India Ltd." 41.39 . . 2 "21-07-2000" "A B B India Ltd." 41.09 . . 2 "24-07-2000" "A B B India Ltd." 39.67 . . 2 "25-07-2000" "A B B India Ltd." 40.19 . . 2 "26-07-2000" "A B B India Ltd." 39.6 . . 2 "27-07-2000" "A B B India Ltd." 40.41 . . 2 "28-07-2000" "A B B India Ltd." 41.32 . . 2 "31-07-2000" "A B B India Ltd." 40.6 . . 2 "01-08-2000" "A B B India Ltd." 40.73 . . 2 "02-08-2000" "A B B India Ltd." 40.59 . . 2 "03-08-2000" "A B B India Ltd." 40.41 . . 2 "04-08-2000" "A B B India Ltd." 39.97 . . 2 "07-08-2000" "A B B India Ltd." 40.39 . . 2 "08-08-2000" "A B B India Ltd." 41.07 . . 2 "09-08-2000" "A B B India Ltd." 41.11 . . 2 "10-08-2000" "A B B India Ltd." 41.17 . . 2 "11-08-2000" "A B B India Ltd." 40.83 . . 2 "14-08-2000" "A B B India Ltd." 40.45 . . 2 "16-08-2000" "A B B India Ltd." 41.19 . . 2 "17-08-2000" "A B B India Ltd." 40.9 . . 2 "18-08-2000" "A B B India Ltd." 41.54 . . 2 "21-08-2000" "A B B India Ltd." 42.14 . . 2 "22-08-2000" "A B B India Ltd." 43.46 . . 2 "23-08-2000" "A B B India Ltd." 45.19 . . 2 "24-08-2000" "A B B India Ltd." 45.92 . . 2 "25-08-2000" "A B B India Ltd." 45.73 . . 2 "28-08-2000" "A B B India Ltd." 45 . . 2 "29-08-2000" "A B B India Ltd." 44.85 . . 2 "30-08-2000" "A B B India Ltd." 44.95 . . 2 "31-08-2000" "A B B India Ltd." 49.15 . . 2 "04-09-2000" "A B B India Ltd." 48.63 . . 2 "05-09-2000" "A B B India Ltd." 48.12 . . 2 "06-09-2000" "A B B India Ltd." 48.39 . . 2 "07-09-2000" "A B B India Ltd." 47.71 . . 2 "08-09-2000" "A B B India Ltd." 46.82 . . 2 "11-09-2000" "A B B India Ltd." 46.19 . . end
With reference to the above data example, I write two codes, which are believed to generate similar results:
Code:
/*******************************************************************************************
Daily_FF_BSE500.do
Purpose: Construct Fama–French (1993)-style daily SMB and HML factors
using BSE500 constituent stocks.
Input : Dataset with variables
stock_id, stock, date, month, pr, rt, mcap (June), bmr (March)
Output : Daily_FF_Factors_BSE500.dta
Contains daily SMB, HML, and six portfolio return series (SL, SM, SH, BL, BM, BH)
********************************************************************************************/
clear all
set more off[
version 17
********************************************************************************
* 1. Prepare dataset
********************************************************************************
* Assuming current data file has: stock_id stock date month pr rt mcap bmr
* Ensure date is Stata daily date and sorted
* Ensure date is Stata daily date and sorted
capture confirm numeric variable date
if _rc == 0 {
format date %td // numeric → just ensure proper date format
}
else {
gen double date = daily(date, "YMD") // string → convert
format date %td
}
sort stock_id date
********************************************************************************
* Compute daily stock returns from adjusted price (pr)
********************************************************************************
* rt = (P_t / P_{t-1}) - 1
bys stock_id (date): gen double rt = (pr / pr[_n-1]) - 1
replace rt = . if rt < -0.9 | rt > 5 // remove obvious outliers if any
* Drop observations missing key variables
drop if missing(stock_id, rt, mcap, bmr)
********************************************************************************
* 2. Assign fiscal year based on June book year convention
********************************************************************************
* Fiscal year (fyear) = year when June mcap is observed
gen int fyear = year(date)
replace fyear = fyear - 1 if month(date) < 7 // portfolio year runs Jul–Jun
********************************************************************************
* 3. Form size and book-to-market groups
********************************************************************************
* Step 3a: Create median (size) and 30th/70th percentile (bmr) cutpoints each fyear
preserve
collapse (median) med_mcap = mcap ///
(p30) p30_bmr = bmr ///
(p70) p70_bmr = bmr, by(fyear)
tempfile breakpoints
save `breakpoints'
restore
* Merge breakpoints into main data
merge m:1 fyear using `breakpoints', nogenerate
* Step 3b: Assign size and book-to-market groups
gen byte june_mcap_group = cond(mcap <= med_mcap, 1, 2) // 1=Small, 2=Big
label define sizegrp 1 "S" 2 "B"
label values june_mcap_group sizegrp
gen byte march_bmr_group = .
replace march_bmr_group = 1 if bmr <= p30_bmr
replace march_bmr_group = 2 if bmr > p30_bmr & bmr <= p70_bmr
replace march_bmr_group = 3 if bmr > p70_bmr
label define bmrgrp 1 "L" 2 "M" 3 "H"
label values march_bmr_group bmrgrp
drop med_mcap p30_bmr p70_bmr
********************************************************************************
* 4. Compute daily value-weighted portfolio returns for 2×3 portfolios
********************************************************************************
* 6 portfolios: SL, SM, SH, BL, BM, BH
gen double mcap_rt = mcap * rt
bys date june_mcap_group march_bmr_group: egen double num = total(mcap_rt)
bys date june_mcap_group march_bmr_group: egen double den = total(mcap)
gen double vw_mean_rt = num / den
* Keep one record per date–portfolio
bys date june_mcap_group march_bmr_group: keep if _n == 1
keep date june_mcap_group march_bmr_group vw_mean_rt
reshape wide vw_mean_rt, i(date) j(june_mcap_group march_bmr_group) string
rename vw_mean_rt11 vw_mean_rt_SL
rename vw_mean_rt12 vw_mean_rt_SM
rename vw_mean_rt13 vw_mean_rt_SH
rename vw_mean_rt21 vw_mean_rt_BL
rename vw_mean_rt22 vw_mean_rt_BM
rename vw_mean_rt23 vw_mean_rt_BH
********************************************************************************
* 5. Construct SMB and HML daily factors
********************************************************************************
gen double SMB = ( (vw_mean_rt_SL + vw_mean_rt_SM + vw_mean_rt_SH)/3 ) ///
- ( (vw_mean_rt_BL + vw_mean_rt_BM + vw_mean_rt_BH)/3 )
gen double HML = ( (vw_mean_rt_SH + vw_mean_rt_BH)/2 ) ///
- ( (vw_mean_rt_SL + vw_mean_rt_BL)/2 )
label var SMB "Small Minus Big (BSE500, daily)"
label var HML "High Minus Low (BSE500, daily)"
********************************************************************************
* 6. Save and summarize results
********************************************************************************
save "Daily_FF_Factors_BSE500.dta", replace
summ SMB HML
corr SMB HML
list date SMB HML in 1/10
display "✅ Daily SMB and HML factors (and 6 portfolio returns) created successfully."
exit
Code - 2
Code:
*Convert month string like "2001m6" to Stata monthly date
gen date1=date(month,"DMY")
format date1 %td
drop month
rename date1 date
gen mdate= mofd(date)
format mdate %tm
* Ensure date is Stata daily date and sorted
capture confirm numeric variable date
if _rc == 0 {
format date %td // numeric → just ensure proper date format
}
else {
gen double date = daily(date, "YMD") // string → convert
format date %td
}
sort stock_id date
********************************************************************************
* Compute daily stock returns from adjusted price (pr)
********************************************************************************
* rt = (P_t / P_{t-1}) - 1
bys stock_id (date): gen double rt = (pr / pr[_n-1]) - 1
replace rt = . if rt < -0.9 | rt > 5 // remove obvious outliers if any
* Drop observations missing key variables
drop if missing(stock_id, rt, mcap, bmr)
gen moy = month(dofm(mdate))
gen year = year(dofm(mdate))
// CREATE A "FISCAL YEAR" RUNNING FROM JULY THROUGH SUBSEQUENT JUNE
gen fyear = cond(moy > 6, year, year-1)
frame put stock_id fyear mcap bmr, into(mcap_bmr_work)
frame change mcap_bmr_work
collapse (count) n_mcap = mcap n_bmr = bmr (firstnm) mcap bmr, by(stock_id fyear)
assert n_mcap <= 1 & n_bmr <= 1 // VERIFY UNIQUE VALUE OF MCAP AND BMR
replace fyear = fyear + 1 // CHANGE THE FYEAR TO WHICH THEY WILL APPLY
frame change default
rename (mcap bmr) orig=
frlink m:1 stock_id fyear, frame(mcap_bmr_work)
frget mcap bmr, from(mcap_bmr_work)
frame drop mcap_bmr_work
drop mcap_bmr_work
egen byte representative = tag(stock_id fyear)
// MEDIAN SPLIT BASED ON JUNE VALUE OF mcap
capture program drop one_year_median_split
program define one_year_median_split
xtile june_mcap_group = mcap, nq(2)
exit
end
frame put stock_id fyear mcap if representative & !missing(mcap), into(median_split) // ***
frame change median_split
runby one_year_median_split, by(fyear)
frame change default
frlink m:1 stock_id fyear, frame(median_split stock_id fyear) // ***
frget june_mcap_group, from(median_split)
frame drop median_split
drop median_split
// NOW SPLIT AT 30TH AND 70TH PERCENTILES OF bmr
capture program drop one_year_three_groups
program define one_year_three_groups
if _N > = 3 {
_pctile bmr, percentiles(30 70)
gen cut = `r(r1)' in 1
replace cut = `r(r2)' in 2
xtile march_bmr_group = bmr, cutpoints(cut)
}
else {
gen march_bmr_group = .
}
exit
end
frame put stock_id fyear bmr if representative & !missing(bmr), into(three_groups) // ***
frame change three_groups
runby one_year_three_groups, by(fyear) verbose
frame change default
frlink m:1 stock_id fyear, frame(three_groups stock_id fyear) // ***
frget march_bmr_group, from(three_groups)
frame drop three_groups
drop three_groups
capture program drop one_weighted_return
program define one_weighted_return
if !missing(june_mcap_group, march_bmr_group) {
egen numerator = total(mcap*rt)
egen denominator = total(mcap)
gen vw_mean_rt = numerator/denominator
}
exit
end
drop if missing(june_mcap_group, march_bmr_group)
runby one_weighted_return, by(date june_mcap_group march_bmr_group)
collapse (first) vw_mean_rt, by(date june_mcap_group march_bmr_group)
drop if missing(vw_mean_rt)
keep date june_mcap_group march_bmr_group vw_mean_rt
isid june_mcap_group march_bmr_group date, sort
by date june_mcap_group, sort: egen temp = mean(vw_mean_rt)
by date (june_mcap_group), sort: gen SMB = temp[1] - temp[_N]
drop temp
by date march_bmr_group, sort: egen temp = mean(vw_mean_rt)
by date (march_bmr_group): gen HML = temp[1] - temp[_N]
drop temp
// AND IF YOU WANT TO REDUCE TO ONE OBSERVATION PER MONTH
label define june_mcap_group 1 "S" 2 "B"
label define march_bmr_group 1 "L" 2 "M" 3 "H"
label values june_mcap_group june_mcap_group
label values march_bmr_group march_bmr_group
decode june_mcap_group, gen (mcap_group)
decode march_bmr_group, gen(bmr_group)
drop june_mcap_group march_bmr_group
egen groups = concat(mcap_group bmr_group)
keep date groups SMB HML vw_mean_rt
rename vw_mean_rt =_
reshape wide vw_mean_rt_, i(date) j(groups) string
gen HML_new = (vw_mean_rt_SH + vw_mean_rt_BH)/2 - (vw_mean_rt_SL + vw_mean_rt_BL)/2
drop HML
rename HML_new HML
When i run each of these codes, i face some incompatibilities that need to be debugged. Further, I want to confirm that do both of these produce similar results and follow same methodology.

Comment