I need to run panel regressions with fixed effects multiple times. Regressions are run (1) for dependent variables : ret1_1, ret1_2, ret1_3, ret1_4, respectively; (2) for group aumrank=3,4, respectively; (3) for group amirank=1,2, respectively.
If I don't use the nlcom command, this can be realized via 'statsby'. However, statsby does not seem to support multiple subsequent commands. So I tried to use 'runby' but couldn't make it work.
Could someone please advise? The codes and sample data are as below.


global datapath `"D:..."'
display `datapath'

/******code of sample data******************/
clear
input long dt float ret byte(news unsche sche) float(to ami) byte jump float(ret1_1 ret1_2 ret1_3 ret1_4 int_retami int_retto int_retsche int_retunsche) int year byte(etfnumid aumrank amirank)
1609666200 -.002180594 1 1 0 .001124295 .005375426 0 .002527068 .001685422 .00173495 .002368695 -.000011722 -2.45e-06 0 -.002180594 2011 4 3 2
1609667100 .002527068 1 1 0 .000629071 .0111055 0 -.000841647 -.000792118 -.000158374 .001038704 .0000280644 1.59e-06 0 .002527068 2011 4 3 2
1609670700 .001197078 0 0 0 .0001091 .03030171 0 -.0008952 -.000098878 -.000890252 .0000939248 .0000362735 1.31e-07 0 0 2011 4 3 2
1609677000 .000098956 1 0 1 .000108709 .002515881 0 -.000593883 -.000217716 -.000217716 -.001089055 2.49e-07 1.08e-08 .000098956 0 2011 4 3 2
1609688700 -.000495909 0 0 0 .000259986 .005285485 0 -.001515249 -.002973243 -.004960328 -.004274337 -2.62e-06 -1.29e-07 0 0 2011 4 3 2
1609752600 -.001515249 1 0 1 .000452209 .009275963 0 -.001457994 -.003445078 -.002759088 -.003440106 -.000014055 -6.85e-07 -.001515249 0 2011 4 3 2
1609753500 -.001457994 1 0 1 .002042456 .001979022 0 -.001987085 -.001301094 -.001982112 -.001092408 -2.89e-06 -2.98e-06 -.001457994 0 2011 4 3 2
1609754400 -.001987085 0 0 0 .000165913 .03326962 0 .00068599 4.97e-06 .000894677 -.0007412 -.00006611 -3.30e-07 0 0 2011 4 3 2
1609755300 .00068599 0 0 0 .00010033 .01898024 0 -.000681018 .000208686 -.00142719 -.000238552 .0000130203 6.88e-08 0 0 2011 4 3 2
1609756200 -.000681018 0 0 0 .000789788 .00239528 0 .000889704 -.000746172 .000442466 .000989063 -1.63e-06 -5.38e-07 0 0 2011 4 3 2
1609757100 .000889704 0 0 0 .000361377 .006832943 0 -.001635876 -.000447238 .0000993591 -.000894677 6.08e-06 3.22e-07 0 0 2011 4 3 2
1609758000 -.001635876 0 0 0 .000343836 .013226113 0 .001188638 .001735235 .0007412 -.000751709 -.000021636 -5.62e-07 0 0 2011 4 3 2
1609758900 .001188638 1 0 1 .000417686 .007901614 0 .000546597 -.000447438 -.001940347 -.002817226 9.39e-06 4.96e-07 .001188638 0 2011 4 3 2
1609759800 .000546597 1 0 1 .000184962 .008200959 0 -.000994036 -.002486945 -.003363823 -.003483457 4.48e-06 1.01e-07 .000546597 0 2011 4 3 2
1609760700 -.000994036 1 0 1 .000189598 .014563915 0 -.001492909 -.002369787 -.002489421 -.002798542 -.000014477 -1.88e-07 -.000994036 0 2011 4 3 2
1609761600 -.001492909 1 1 0 .0000598849 .06935453 0 -.000876878 -.000996512 -.001305633 -.000498132 -.00010354 -8.94e-08 0 -.001492909 2011 4 3 2
1609762500 -.000876878 1 1 0 .000314452 .00776471 0 -.000119634 -.000428754 .000378746 .001225423 -6.81e-06 -2.76e-07 0 -.000876878 2011 4 3 2
1609763400 -.000119634 1 1 0 .0000847997 .003928725 0 -.000309121 .00049838 .001345057 .001275358 -4.70e-07 -1.01e-08 0 -.000119634 2011 4 3 2
1609764300 -.000309121 1 1 0 .0000668678 .012877663 0 .000807501 .001654178 .001584478 .002301153 -3.98e-06 -2.07e-08 0 -.000309121 2011 4 3 2
1692287100 -.002313923 0 0 0 .000130497 .024277354 0 0 .00212581 .002724087 .002492538 -.000056176 -3.02e-07 0 0 2013 8 3 2
1692523800 0 1 0 1 .000250993 0 0 .00212581 .002724087 .002492538 .001565808 0 0 0 0 2013 8 3 2
1692524700 .00212581 1 0 1 .000117518 .02476693 0 .000598277 .000366728 -.000560002 -.000560002 .0000526498 2.50e-07 .00212581 0 2013 8 3 2
1692525600 .000598277 0 0 0 9.79e-06 .08364372 0 -.000231548 -.001158279 -.001158279 -.000791345 .0000500421 5.86e-09 0 0 2013 8 3 2
1692526500 -.000231548 0 0 0 .0000273759 .01157619 0 -.00092673 -.00092673 -.000559797 -.002651189 -2.68e-06 -6.34e-09 0 0 2013 8 3 2
1692527400 -.00092673 0 0 0 .0000660284 .01922725 0 0 .000366934 -.001724458 -.002338015 -.000017818 -6.12e-08 0 0 2013 8 3 2
1692528300 . 0 0 0 . . 0 .000366934 -.001724458 -.002338015 -.001747678 . . . . 2013 8 3 2
1692529200 .000366934 1 0 1 .0000934752 .005375589 0 -.002091392 -.002704949 -.002114611 -.002242327 1.97e-06 3.43e-08 .000366934 0 2013 8 3 2
1692530100 -.002091392 1 0 1 .0000760993 .03771361 0 -.000613557 -.000023219 -.000150935 -.000286409 -.000078874 -1.59e-07 -.002091392 0 2013 8 3 2
1692531000 -.000613557 1 0 1 .0000368794 .02284443 0 .000590338 .000462622 .000327148 .001157121 -.000014016 -2.26e-08 -.000613557 0 2013 8 3 2
1692531900 .000590338 1 0 1 .0000407801 .019865755 0 -.000127716 -.000263189 .000566783 7.74e-06 .0000117275 2.41e-08 .000590338 0 2013 8 3 2
1692612000 .001062853 1 1 0 .000156028 .00933105 0 .000096567 .00187945 .002025952 .002604043 9.92e-06 1.66e-07 0 .001062853 2013 8 3 2
1692612900 .000096567 1 1 0 .0000551064 .002400191 0 .001782883 .001929385 .002507476 .001352889 2.32e-07 5.32e-09 0 .000096567 2013 8 3 2
1692613800 .001782883 1 1 0 .000124255 .01961789 0 .000146503 .000724593 -.000429993 .000223601 .0000349764 2.22e-07 0 .001782883 2013 8 3 2
1692614700 .000146503 0 0 0 .0000301418 .006644412 0 .00057809 -.000576496 .000077098 .000221641 9.73e-07 4.42e-09 0 0 2013 8 3 2
1692615600 .00057809 0 0 0 .0000735461 .010739053 0 -.001154587 -.000500992 -.00035645 -.000366085 6.21e-06 4.25e-08 0 0 2013 8 3 2
1609932600 -.000771374 1 0 1 .00287378 .0000311048 0 .000257191 -.000171497 -.000171497 .000771374 -2.40e-08 -2.22e-06 -.000771374 0 2011 9 4 1
1609933500 .000257191 0 0 0 .001743209 .0000170927 0 -.000428688 -.000428688 .000514183 .000633264 4.40e-09 4.48e-07 0 0 2011 9 4 1
1609934400 -.000428688 0 0 0 .002290391 .0000216932 0 0 .000942871 .001061952 0 -9.30e-09 -9.82e-07 0 0 2011 9 4 1
1609935300 0 0 0 0 .000890945 0 0 .000942871 .001061952 0 0 0 0 0 0 2011 9 4 1
1609936200 .000942871 0 0 0 .001725612 .000063269 0 .000119081 -.000942871 -.000942871 -.000342759 5.97e-08 1.63e-06 0 0 2011 9 4 1
1609937100 .000119081 0 0 0 .001120097 .0000123089 0 -.001061952 -.001061952 -.000461841 .000822918 1.47e-09 1.33e-07 0 0 2011 9 4 1
1609938000 -.001061952 0 0 0 .002189755 .0000562083 0 0 .000600112 .00188487 .00171365 -5.97e-08 -2.33e-06 0 0 2011 9 4 1
1609938900 0 0 0 0 .001325774 0 0 .000600112 .00188487 .00171365 .001628037 0 0 0 0 2011 9 4 1
1609939800 .000600112 0 0 0 .000935329 .0000743187 0 .001284759 .001113538 .001027925 -8.57e-07 4.46e-08 5.61e-07 0 0 2011 9 4 1
1609940700 .001284759 0 0 0 .000942965 .000157615 0 -.000171221 -.000256833 -.001285616 -.001627636 2.02e-07 1.21e-06 0 0 2011 9 4 1
1609941600 -.000171221 0 0 0 .001539384 .0000128693 0 -.000085613 -.001114395 -.001456415 -.000428137 -2.20e-09 -2.64e-07 0 0 2011 9 4 1
1609942500 -.000085613 0 0 0 .000573713 .0000172675 0 -.001028782 -.001370802 -.000342524 -.001199349 -1.48e-09 -4.91e-08 0 0 2011 9 4 1
1609943400 -.001028782 0 0 0 .001452696 .0000820313 0 -.00034202 .000686258 -.000170567 .000686258 -8.44e-08 -1.49e-06 0 0 2011 9 4 1
1609944300 -.00034202 0 0 0 .002366901 .0000167437 0 .001028278 .000171453 .001028278 .001028278 -5.73e-09 -8.10e-07 0 0 2011 9 4 1
1609945200 .001028278 0 0 0 .001476578 .0000806096 0 -.000856825 0 0 3.42e-08 8.29e-08 1.52e-06 0 0 2011 9 4 1
1609946100 -.000856825 0 0 0 .003282025 .0000302451 0 .000856825 .000856825 .000856859 -.000684008 -2.59e-08 -2.81e-06 0 0 2011 9 4 1
1609947000 .000856825 0 0 0 .001758913 .0000563872 0 0 3.42e-08 -.001540832 -.00008554 4.83e-08 1.51e-06 0 0 2011 9 4 1
1609947900 0 0 0 0 .00512868 0 0 3.42e-08 -.001540832 -.00008554 .000171057 0 0 0 0 2011 9 4 1
1610013600 .001455293 1 1 0 .00358266 .0000469632 0 .000256597 -.001369629 -.001969432 -.002055147 6.83e-08 5.21e-06 0 .001455293 2011 9 4 1
1610014500 .000256597 1 1 0 .002846506 .0000104193 0 -.001626225 -.002226028 -.002311744 -.003770033 2.67e-09 7.30e-07 0 .000256597 2011 9 4 1
1610015400 -.001626225 1 1 0 .003627558 .0000519008 0 -.000599803 -.000685518 -.002143807 -.003088541 -8.44e-08 -5.90e-06 0 -.001626225 2011 9 4 1
1610016300 -.000599803 1 1 0 .004677037 .0000148561 0 -.000085716 -.001544004 -.002488738 -.002488738 -8.91e-09 -2.81e-06 0 -.000599803 2011 9 4 1
1610017200 -.000085716 0 0 0 .004742384 2.09e-06 0 -.001458289 -.002403022 -.002403022 -.003004681 -1.79e-10 -4.06e-07 0 0 2011 9 4 1
1610018100 -.001458289 0 0 0 .00370888 .0000456183 0 -.000944733 -.000944733 -.001546392 -.00266472 -6.65e-08 -5.41e-06 0 0 2011 9 4 1
1610019000 -.000944733 0 0 0 .005087634 .0000215646 0 0 -.000601659 -.001719987 -.003322234 -2.04e-08 -4.81e-06 0 0 2011 9 4 1
1610019900 0 0 0 0 .002857035 0 0 -.000601659 -.001719987 -.003322234 -.002494947 0 0 0 0 2011 9 4 1
1610020800 -.000601659 0 0 0 .000934937 .0000747787 0 -.001118328 -.002720575 -.001893288 -.001979432 -4.50e-08 -5.63e-07 0 0 2011 9 4 1
1610021700 -.001118328 0 0 0 .002839654 .0000458141 0 -.001602247 -.00077496 -.000861104 .0000860696 -5.12e-08 -3.18e-06 0 0 2011 9 4 1
1610022600 -.001602247 0 0 0 .024852196 7.51e-06 0 .000827287 .000741143 .001688317 .002548606 -1.20e-08 -.000039819 0 0 2011 9 4 1
1610023500 .000827287 0 0 0 .00608566 .0000158263 0 -.000086144 .00086103 .001721319 .001119194 1.31e-08 5.03e-06 0 0 2011 9 4 1
1610024400 -.000086144 0 0 0 .00283245 3.54e-06 0 .000947174 .001807463 .001205338 .001807463 -3.05e-10 -2.44e-07 0 0 2011 9 4 1
1610025300 .000947174 1 1 0 .003012407 .0000365741 0 .000860289 .000258165 .000860289 .001290156 3.46e-08 2.85e-06 0 .000947174 2011 9 4 1
1610026200 .000860289 1 1 0 .003948831 .0000253198 0 -.000602125 0 .000429867 .000773602 2.18e-08 3.40e-06 0 .000860289 2011 9 4 1
1610027100 -.000602125 1 1 0 .003316151 .0000211153 0 .000602125 .001031992 .001375727 .001461674 -1.27e-08 -2.00e-06 0 -.000602125 2011 9 4 1
1610028000 .000602125 0 0 0 .003355994 .0000208521 0 .000429867 .000773602 .00085955 .000945464 1.26e-08 2.02e-06 0 0 2011 9 4 1
1610028900 .000429867 0 0 0 .003425706 .0000145775 0 .000343735 .000429683 .000515597 .000601504 6.27e-09 1.47e-06 0 0 2011 9 4 1
1610029800 .000343735 0 0 0 .001377267 .0000289837 0 .0000859475 .000171862 .000257769 .001459692 9.96e-09 4.73e-07 0 0 2011 9 4 1
1610030700 .0000859475 0 0 0 .001588052 6.28e-06 0 .0000859143 .000171821 .001373745 .001459541 5.40e-10 1.36e-07 0 0 2011 9 4 1
1610031600 .0000859143 0 0 0 .00146709 6.80e-06 0 .000085907 .00128783 .001373627 .00085692 5.84e-10 1.26e-07 0 0 2011 9 4 1
1610032500 .000085907 0 0 0 .003985741 2.50e-06 0 .001201923 .00128772 .000771013 -.003199301 2.15e-10 3.42e-07 0 0 2011 9 4 1
1610033400 .001201923 0 0 0 .004587512 .0000303819 0 .0000857964 -.00043091 -.004401225 -.002586734 3.65e-08 5.51e-06 0 0 2011 9 4 1
1610034300 .0000857964 0 0 0 .005537546 1.80e-06 0 -.000516707 -.004487021 -.002672531 -.003017894 1.54e-10 4.75e-07 0 0 2011 9 4 1
1610272800 .001814491 0 0 0 .004811677 .0000439974 0 -.000345363 .000604099 .000172637 0 7.98e-08 8.73e-06 0 0 2011 9 4 1
1610273700 -.000345363 0 0 0 .0095684 4.21e-06 0 .000949463 .000518001 .000345364 .001121996 -1.45e-09 -3.30e-06 0 0 2011 9 4 1
1610274600 .000949463 0 0 0 .004819976 .0000229688 0 -.000431462 -.000604099 .000172533 .000603735 2.18e-08 4.58e-06 0 0 2011 9 4 1
1610275500 -.000431462 0 0 0 .003594783 .0000140011 0 -.000172637 .000603995 .001035197 .002413786 -6.04e-09 -1.55e-06 0 0 2011 9 4 1
1610276400 -.000172637 1 0 1 .002481471 8.12e-06 0 .000776632 .001207834 .002586423 .002155824 -1.40e-09 -4.28e-07 -.000172637 0 2011 9 4 1
1610277300 .000776632 1 0 1 .002075605 .0000436216 0 .000431202 .001809791 .001379192 .002068058 3.39e-08 1.61e-06 .000776632 0 2011 9 4 1
1610278200 .000431202 1 0 1 .001699338 .0000295695 0 .001378589 .00094799 .001636856 .00180902 1.28e-08 7.33e-07 .000431202 0 2011 9 4 1
1610279100 .001378589 1 0 1 .003114678 .0000515071 0 -.000430599 .000258267 .000430431 -.000175657 7.10e-08 4.29e-06 .001378589 0 2011 9 4 1
1610280000 -.000430599 0 0 0 .001723057 .0000290941 0 .000688866 .00086103 .000254942 .001033147 -1.25e-08 -7.42e-07 0 0 2011 9 4 1
1610280900 .000688866 0 0 0 .004363455 .000018367 0 .000172164 -.000433924 .000344281 .000258226 1.27e-08 3.01e-06 0 0 2011 9 4 1
1610281800 .000172164 0 0 0 .001737181 .000011528 0 -.000606088 .000172117 .0000860622 .0000869228 1.98e-09 2.99e-07 0 0 2011 9 4 1
1610282700 -.000606088 1 1 0 .001463373 .0000482061 0 .000778205 .00069215 .000693011 .000175666 -2.92e-08 -8.87e-07 0 -.000606088 2011 9 4 1
1610283600 .000778205 1 1 0 .001080038 .0000837989 0 -.000086055 -.000085194 -.000602539 -.001119291 6.52e-08 8.40e-07 0 .000778205 2011 9 4 1
1610284500 -.000086055 1 1 0 .002473997 4.05e-06 0 8.61e-07 -.000516484 -.001033236 -.000431246 -3.48e-10 -2.13e-07 0 -.000086055 2011 9 4 1
1610285400 8.61e-07 0 0 0 .000893851 1.12e-07 0 -.000517345 -.001034096 -.000432107 -.000345154 9.64e-14 7.69e-10 0 0 2011 9 4 1
1610286300 -.000517345 0 0 0 .000687728 .0000875404 0 -.000516751 .0000852383 .000172191 .000946685 -4.53e-08 -3.56e-07 0 0 2011 9 4 1
1610287200 -.000516751 0 0 0 .002884728 .0000208567 0 .00060199 .000688943 .001463436 .001377411 -1.08e-08 -1.49e-06 0 0 2011 9 4 1
1610288100 .00060199 0 0 0 .000938586 .0000746316 0 .0000869528 .000861446 .000775421 .001549382 4.49e-08 5.65e-07 0 0 2011 9 4 1
1610289000 .0000869528 0 0 0 .000827923 .0000122198 0 .000774493 .000688468 .001462429 .000774493 1.06e-09 7.20e-08 0 0 2011 9 4 1
1610289900 .000774493 0 0 0 .001850534 .000048658 0 -.000086025 .000687935 0 -.0004302 3.77e-08 1.43e-06 0 0 2011 9 4 1
1610290800 -.000086025 0 0 0 .002227571 4.49e-06 0 .000773961 .0000860252 -.000344175 .000170229 -3.86e-10 -1.92e-07 0 0 2011 9 4 1
1610291700 .000773961 0 0 0 .001962225 .0000458253 0 -.000687935 -.001118135 -.000603732 -.001437198 3.55e-08 1.52e-06 0 0 2011 9 4 1
1610292600 -.000687935 0 0 0 .001778497 .0000449706 0 -.0004302 .0000842033 -.000749263 -.000172974 -3.09e-08 -1.22e-06 0 0 2011 9 4 1
end

/*********single regression with nlcom*****/
use "$datapath\subtest.dta",clear
xtset etfnumid dt
set matsize 5400
xtreg ret1_4 ret int_retsche int_retunsche sche unsche int_retto to int_retami ami i.year if jump ==1,fe vce(robust)

nlcom (sche:_b[ret]+_b[int_retsche ]) (unsche:_b[ret]+_b[int_retunsche ])(unsche_sche:_b[int_retunsche]-_b[int_retsche])
return list
return list
mat b1=r(b)
mat v1=r(V)

local sche=b1[1,1]
local sesche=sqrt(v1[1,1])
local zsche = `sche'/`sesche'
local psche = 2*normal(-abs(`zsche'))

local unsche=b1[1,2]
local seunsche=sqrt(v1[2,2])
local zunsche = `unsche'/`seunsche'
local punsche = 2*normal(-abs(`zunsche'))


outreg2 using fe5.doc, keep(ret int_retsche int_retunsche sche unsche int_retto to int_retami ami) nocons /*
*/adds(schen, `sche', psche,`psche', unschen, `unsche', punsche,`punsche') /*
*/ tstat bdec(4) tdec(2) rdec(4) adec(4) addtext(Firm FE, Yes, Year FE, Yes)
/************************************************** **/



//////////////////////group regression without nlcom using statsby//////////////////////////////
foreach depid of numlist 1/4 {
foreach groupid in aumrank amirank{
use "$datapath\subtest.dta",clear
xtset etfnumid dt
set matsize 5400

di "`groupid'"
sort `groupid'
statsby _b _se Rsq=e(r2) adjRsq=e(r2_a) nobs=e(N) , by(`groupid') saving($datapath\by_`groupid'.dta, replace):/*
*/xtreg ret1_`depid' ret int_retsche int_retunsche sche unsche int_retto to int_retami ami i.year if jump ==1,fe vce(robust)


use "$datapath\by_`groupid'.dta",clear
cls
local regid "5"
gen regid= "`regid'"
gen depid= "`depid'"
gen groupid= "`groupid'"
rename `groupid' rank
keep _b_* _se_* _eq2* regid depid groupid rank
save group_`groupid'_`regid'_`depid'toami.dta, replace
}
}
///////////////////////////////////////////////////////////////////////////////////


/******************test code using runby with bugs, not working**********************************/

capture program drop group_regression
program define group_regression
use "$datapath\subtest.dta",clear
xtset etfnumid dt
set matsize 5400
xtreg ret1_`depid' ret int_retsche int_retunsche sche unsche int_retto to int_retami ami i.year if jump ==1,fe vce(robust)

nlcom (sche:_b[ret]+_b[int_retsche ]) (unsche:_b[ret]+_b[int_retunsche ])(unsche_sche:_b[int_retunsche]-_b[int_retsche])
return list
return list
mat b1=r(b)
mat v1=r(V)

local sche=b1[1,1]
local sesche=sqrt(v1[1,1])
local zsche = `sche'/`sesche'
local psche = 2*normal(-abs(`zsche'))

local unsche=b1[1,2]
local seunsche=sqrt(v1[2,2])
local zunsche = `unsche'/`seunsche'
local punsche = 2*normal(-abs(`zunsche'))


rename `groupid' rank
keep _b_* _se_* _eq2* rank , `sche', `psche', `unsche', `punsche'

exit

end

foreach depid of numlist 1/4 {
use "$datapath\subtest.dta",clear
xtset etfnumid dt
set matsize 5400
local groupid aumrank
di "`groupid'"
local regid "5"


runby group_regression, by(`groupid') status
save group_`groupid'_`regid'_`depid'toami.dta, replace }



/************************************************** **/