Here, due to nature of my data I need to collapse my repeated cross section data to panel data ( the following sample shows my collapsed data at county and year level ). I need to run the first stage of IV regression on this collapsed panel data on year and County level. Then I need to store the first stage , to run the second stage on my regular repeated cross section data.

I need to do this since if a county is overly populated then survey observation from that county will bias my IV result since more observation will be from that county. Therefore , I need to collapse to accomplish the first stage of IV.

Here, dep_var is a variable that's converted into logarithmic form, endogen_var is my endogenous variable . I have two instrumental variable - IV_one and IV_two. My endogenous variable and IV_two are binary dummy. But, IV_one takes up multiple values ( ranging from 1-3)

Can anyone kindly guide me what commands to run to do the above approach ?

My ivregress looks like this:

Code:
ivregress 2sls dep_var (endogen_var=IV_one#IV_two) male ismarried wasmarried age age2 black asian hispanic lths hsdegree somecollege i.year i.IV_one i.IV_two  i.county , cluster(county) first
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input float dep_var int year double county float(endogen_var IV_one IV_two male ismarried wasmarried age age2 black asian hispanic lths hsdegree somecollege)
 9.999117 2013 10003 1 1 1  .4589878 .52443284  .15445027  41.98604  1944.672   .2678883 .066317625  .12041885  .09685864  .2897033 .26876092
 10.16623 2006 10003 0 1 0  .4700499  .5657238  .14559068  39.98419 1757.8794  .22545756  .05990016  .09317803  .13061564  .3169717 .27121463
 10.11985 2020 10003 1 1 1 .51072127 .53216374   .1325536  42.11696  1959.041  .23391813   .0779727  .11111111 .074074075   .294347 .22807017
 10.09567 2009 10003 0 1 0 .47560975 .57229966   .1358885  40.77874 1822.1376    .228223  .05139373   .0879791  .09059233  .2979094 .26567945
 10.02907 2011 10003 1 1 0  .4818038 .52373415   .1431962  40.24367 1791.8386  .23338607  .05221519  .10838608  .11234177  .3156646 .27373418
 9.984045 2012 10003 1 1 1  .4660596 .52483445  .15066226  40.99917 1860.7905   .2673841  .06043046       .125  .12168874
  9.94321 2017 12057 0 1 0  .4686717  .5112782  .12531328  40.12281  1779.857  .14786968   .0726817   .2982456 .067669176  .3283208 .23809524
10.033404 2016 12057 0 1 0    .46875  .4921875  .15885417  41.88281  1928.138   .1640625  .05729167   .3072917  .04947917  .3072917 .27864584
 10.12466 2021 12057 1 1 1 .50442475  .5162242  .14454277  41.51033  1884.826  .14159292   .0619469   .4424779 .064896755  .3156342 .25958702
10.158172 2019 12057 1 1 1  .4940334  .5393795   .1718377   40.7327 1832.3174  .14081146  .06205251   .3556086  .09307876 .28400955  .2673031
 9.985887 2015 12057 0 1 0  .4722955   .530343  .14775726  40.43008 1810.0132   .1741425  .04221636   .3139842  .07387863  .3218997  .2453826
 10.07322 2020 12057 1 1 1  .4801325 .49668875  .18543047  41.85431  1929.927  .16225165  .07284768   .3940397  .08609272 .24
10.122314 2009 18063 1 1 0 .44444445  .6944444  .11111111  43.44444   2088.25          0  .01388889  .01388889  .05555556 .30555555  .4027778
10.343414 2020 20091 1 1 1  .4747899  .6554622  .11344538  41.44538  1881.269 .071428575  .08403362   .1092437  .03781513 .12605043 .20588236
10.273802 2021 20091 1 1 1  .4718615  .6147186   .0995671  41.07359  1855.117  .03030303 .064935066  .12987013  .04329005 .11688311  .2164502
10.339568 2019 20091 1 1 1  .4730769  .6269231   .1423077  40.26154  1765.823  .08076923 .023076924   .1076923  .06538462 .11923077 .21923077
 10.27767 2018 20091 1 1 1  .4763636  .6072727  .14545454  40.68727 1810.7236  .06909091 .025454545  .09818182  .07636364 .145
10.001433 2015 12001 0 1 0        .5  .4285714 .10714286 38.96429 1733.1786        .25   .17857143          0          0       .25       .25
  9.72764 2006 12005 0 2 0  .4821429 .58928573  .2142857 41.23214 1885.3036       .125  .035714287       .125  .19642857       .25      .375
10.269828 2018 12005 0 2 0  .5254237  .6271186 .13559322 39.83051  1733.017  .18644068           0  .08474576  .05084746  .2542373  .3220339
10.068893 2008 12005 0 2 0 .53731346  .5970149  .2238806 42.46269 1961.5374  .08955224           0  .08955224 .029850746  .4179105  .4179105
 9.974488 2007 12005 0 2 0  .5416667  .5972222 .20833333 41.20833 1883.7084  .15277778   .01388889  .06944445  .05555556  .3888889  .4027778
 9.98806 2016 12009 0 1 0  .4545455  .5041322 .20661157  42.3719  1982.157   .1322314   .02479339  .20661157  .07438017  .3719008 .32231405
 10.04933 2017 12009 1 1 1  .4778761  .6017699 .15929204 42.90266 1999.6637  .14159292 .0088495575  .11504425  .04424779   .300885  .3362832
 10.16503 2012 12009 0 1 1   .480916  .5648855 .22137405 44.22901 2147.1755  .10687023  .015267176  .08396947  .06870229 .29007635 .29007635
 10.19963 2019 12009 1 1 1  .6020408  .5408163 .13265306 43.21429 2085.6633   .1632653           0   .1122449 .030612245 .14285715  .4489796
 9.983001 2013 12009 1 1 1 .4890511  .5912409 .13868614 41.88321 1932.9343  .08029197   .09489051  .09489051  .03649635 .23357664  .3576642
 10.12681 2008 12009 0 1 0  .4722222  .6736111 .11805555 43.47222 2037.0695  .06944445   .02777778  .13194445  .07638889 .215277
9.510912 2006 48141 0 3 0  .4918033  .6680328  .12295082  38.55738  1660.213  .020491803  .020491803  .8647541   .3032787 .24590164 .28278688
 9.691033 2012 48141 0 3 0  .4596491  .5754386  .14736842  39.83509 1783.0842  .014035088  .007017544  .8807018         .2  .3052632 .27719298
 9.956921 2015 48141 0 3 0  .4732143 .51785713   .1517857  39.45536 1756.5803  .017857144           0  .9285714  .25892857  .2767857 .23214285
 9.749756 2014 48141 0 3 0  .4222222 .56666666  .15555556      40.5 1830.1593   .04814815 .0037037036        .9   .1962963 .27407408  .3259259
 9.540812 2007 48141 0 3 0  .4552529  .5992218   .1634241 37.976654 1605.5098  .023346303  .007782101  .9027237  .29182878 .28015563
end