Hello, thank you in advance for any advice you are able to provide me with. I am a rather novice when it comes to coding, so feel free to comment on mistakes in my methodology.

Here is an example of some of the data I am working with for reference.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long PlantID str2 PlantState double NameplateCapacityMW int OperatingYear str25 County float Year
60127 "VA"   80 2016 "Accomack" 2016
60127 "VA"   80 2016 "Accomack" 2017
60127 "VA"   80 2016 "Accomack" 2018
60127 "VA"   80 2016 "Accomack" 2019
60445 "ID"   40 2016 "Ada"      2016
60445 "ID"   40 2016 "Ada"      2017
60010 "ID"   20 2017 "Ada"      2018
60445 "ID"   40 2016 "Ada"      2019
60445 "ID"   40 2016 "Ada"      2016
60010 "ID"   20 2017 "Ada"      2017
60010 "ID"   20 2017 "Ada"      2018
60445 "ID"   40 2016 "Ada"      2019
59850 "CO"  1.2 2015 "Adams"    2017
59850 "CO"  1.2 2015 "Adams"    2018
59144 "CO"  1.8 2012 "Adams"    2019
59850 "CO"  1.2 2015 "Adams"    2016
60725 "CO"  1.5 2017 "Adams"    2017
59849 "CO"  1.2 2015 "Adams"    2018
59144 "CO"  1.8 2012 "Adams"    2019
59849 "CO"  1.2 2015 "Adams"    2015
60726 "CO"  1.8 2017 "Adams"    2017
59144 "CO"  1.8 2012 "Adams"    2018
61563 "CO"  1.6 2017 "Adams"    2019
60764 "CO" 12.8 2016 "Adams"    2016
60764 "CO" 12.8 2016 "Adams"    2018
59144 "CO"  1.8 2012 "Adams"    2019
59850 "CO"  1.2 2015 "Adams"    2016
60726 "CO"  1.8 2017 "Adams"    2017
61439 "CO"  1.3 2017 "Adams"    2018
61439 "CO"  1.3 2017 "Adams"    2019
59144 "CO"  1.8 2012 "Adams"    2012
59849 "CO"  1.2 2015 "Adams"    2016
59850 "CO"  1.2 2015 "Adams"    2017
61753 "CO"  1.5 2018 "Adams"    2018
60764 "CO" 12.8 2016 "Adams"    2019
59850 "CO"  1.2 2015 "Adams"    2015
59144 "CO"  1.8 2012 "Adams"    2016
59849 "CO"  1.2 2015 "Adams"    2017
59850 "CO"  1.2 2015 "Adams"    2018
59849 "CO"  1.2 2015 "Adams"    2019
61563 "CO"  1.6 2017 "Adams"    2018
60726 "CO"  1.8 2017 "Adams"    2019
60725 "CO"  1.5 2017 "Adams"    2018
61439 "CO"  1.3 2017 "Adams"    2019
59850 "CO"  1.2 2015 "Adams"    2016
59849 "CO"  1.2 2015 "Adams"    2017
59144 "CO"  1.8 2012 "Adams"    2018
61563 "CO"  1.6 2017 "Adams"    2019
58935 "VT"    2 2013 "Addison"  2013
58935 "VT"    2 2013 "Addison"  2014
61340 "VT"  2.2 2017 "Addison"  2017
61340 "VT"  2.2 2017 "Addison"  2018
61340 "VT"  2.2 2017 "Addison"  2019
58935 "VT"    2 2013 "Addison"  2013
61340 "VT"  2.2 2017 "Addison"  2017
58935 "VT"    2 2013 "Addison"  2018
60562 "VT"  4.9 2016 "Addison"  2019
58935 "VT"    2 2013 "Addison"  2013
60562 "VT"  4.9 2016 "Addison"  2016
58935 "VT"    2 2013 "Addison"  2017
60562 "VT"  4.9 2016 "Addison"  2018
58935 "VT"    2 2013 "Addison"  2019
58618 "FL"  1.2 2010 "Alachua"  2010
58618 "FL"  1.2 2010 "Alachua"  2011
58618 "FL"  1.2 2010 "Alachua"  2012
57438 "FL"  1.6 2011 "Alachua"  2013
58618 "FL"  1.2 2010 "Alachua"  2014
57438 "FL"  1.6 2011 "Alachua"  2015
57438 "FL"  1.6 2011 "Alachua"  2016
58618 "FL"  1.2 2010 "Alachua"  2017
58618 "FL"  1.2 2010 "Alachua"  2018
57438 "FL"  1.6 2011 "Alachua"  2019
57438 "FL"  1.6 2011 "Alachua"  2011
58618 "FL"  1.2 2010 "Alachua"  2012
58618 "FL"  1.2 2010 "Alachua"  2013
58618 "FL"  1.2 2010 "Alachua"  2014
58618 "FL"  1.2 2010 "Alachua"  2015
58618 "FL"  1.2 2010 "Alachua"  2016
57438 "FL"  1.6 2011 "Alachua"  2017
58618 "FL"  1.2 2010 "Alachua"  2018
57438 "FL"  1.6 2011 "Alachua"  2019
58739 "NC"    3 2016 "Alamance" 2016
58740 "NC"    3 2016 "Alamance" 2017
59406 "NC"    2 2015 "Alamance" 2018
60367 "NC"  5.2 2016 "Alamance" 2019
61527 "NC"    5 2017 "Alamance" 2017
60367 "NC"  5.2 2016 "Alamance" 2018
58739 "NC"    3 2016 "Alamance" 2019
58739 "NC"    3 2016 "Alamance" 2016
61527 "NC"    5 2017 "Alamance" 2017
59406 "NC"    2 2015 "Alamance" 2018
58725 "NC"    5 2015 "Alamance" 2019
58739 "NC"    3 2016 "Alamance" 2016
58740 "NC"    3 2016 "Alamance" 2017
58725 "NC"    5 2015 "Alamance" 2018
61527 "NC"    5 2017 "Alamance" 2019
58725 "NC"    5 2015 "Alamance" 2015
58725 "NC"    5 2015 "Alamance" 2016
58725 "NC"    5 2015 "Alamance" 2017
58725 "NC"    5 2015 "Alamance" 2018
end



I am trying to create a new dataframe that contains sums of each capacity type (for example, variable "NameplateCapacityMW") by year for each unique County entry. The idea of the algorithm is that within a given County, for Year x (say, 2016) all unique Plants within the given County with OperatingYear <= 2016 are summed, with this sum recorded for that year in some variable (AnnCntyNmCap).

Basically, for example, my first few entries of the desired output would be:

County Year AnnCntyNmCap
Accomack 2016 80
Accomack 2017 80
Accomack 2018 80
Accomack 2019 80
Ada 2016 40
Ada 2017 60
Ada 2018 60
Ada 2019 60

Notice that for Ada, in year 2017 a value of 20 is added because a second generator opened in 2017 (denoted by the second plant having an "OperatingYear" of 2017)

Here was my initial idea for implementing the algorithm I described above:


gen AnnCntyNmCap = .

levelsof County, local(counties)

foreach i of local counties {
forvalues of j = i {
replace AnnCntyNmCap = AnnCntyNmCap + ((Year-YrEntrdServ+1)*NmPltCap_MW)
}
}


Because my ultimate goal is to have one line per Year per County, I expect to have to use the collapse command at some point, but the above code does not execute so it will take some further development before I will worry about that.

Thank you for taking the time to look over my issue, please let me know if there is anything I can elaborate on to better convey my problem or goal.