Hi Stata users,

I am trying to generate a new variable (Industry_revenue) for the data example shown below. I want to calculate the total revenue (Rev) in each industry (SIC2) in each year and then, insert the result as the new variable (Industry_revenue) in front of each observation (firmid) considering their specific industry.

I would be grateful if someone helps me in this regard.

Kind regards,
Mahmoud


Example:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str14 firmid int year double rev byte SIC2
"SE0000101297" 2013 664.2 24
"SE0000101297" 2014 666.3 24
"SE0000101297" 2015 853.4 24
"SE0000101297" 2016 894.3 24
"SE0000101297" 2017 1243.8 24
"SE0000101297" 2018 2273.25 24
"SE0000101362" 2013 7666 50
"SE0000101362" 2014 7648 50
"SE0000101362" 2015 7903 50
"SE0000101362" 2016 7821 50
"SE0000101362" 2017 8277 50
"SE0000101362" 2018 3833 50
"SE0000101669" 2013 . 36
"SE0000101669" 2014 . 36
"SE0000101669" 2015 . 36
"SE0000101669" 2016 . 36
"SE0000101669" 2017 . 36
"SE0000101669" 2018 . 36
"SE0000101891" 2013 . 22
"SE0000101891" 2014 . 22
"SE0000101891" 2015 . 22
"SE0000101891" 2016 . 22
"SE0000101891" 2017 . 22
"SE0000101891" 2018 . 22
"SE0000102006" 2013 . 39
"SE0000102006" 2014 . 39
"SE0000102006" 2015 . 39
"SE0000102006" 2016 . 39
"SE0000102006" 2017 . 39
"SE0000102006" 2018 . 39
"SE0000102824" 2013 467.8 44
"SE0000102824" 2014 473.8 44
"SE0000102824" 2015 810 44
"SE0000102824" 2016 983.4 44
"SE0000102824" 2017 827.5 44
"SE0000102824" 2018 1052.9 44
"SE0000103699" 2013 2429.7 38
"SE0000103699" 2014 2622.4 38
"SE0000103699" 2015 3043.8 38
"SE0000103699" 2016 3149.2 38
"SE0000103699" 2017 3448.4 38
"SE0000103699" 2018 3760.7 38
"SE0000103814" 2013 109151 36
"SE0000103814" 2014 112143 36
"SE0000103814" 2015 123511 36
"SE0000103814" 2016 121093 36
"SE0000103814" 2017 122060 36
"SE0000103814" 2018 124129 36
"SE0000104788" 2013 1841.594 39
"SE0000104788" 2014 237.736 39
"SE0000104788" 2015 451.345 39
"SE0000104788" 2016 486.455 39
"SE0000104788" 2017 540.042 39
"SE0000104788" 2018 572.705 39
"SE0000105116" 2013 . .
"SE0000105116" 2014 . .
"SE0000105116" 2015 . .
"SE0000105116" 2016 . .
"SE0000105116" 2017 . .
"SE0000105116" 2018 . .
"SE0000105199" 2013 3920 37
"SE0000105199" 2014 4380 37
"SE0000105199" 2015 4777 37
"SE0000105199" 2016 4374 37
"SE0000105199" 2017 4462 37
"SE0000105199" 2018 5119 37
"SE0000105264" 2013 875.016 28
"SE0000105264" 2014 962.429 28
"SE0000105264" 2015 . 28
"SE0000105264" 2016 . 28
"SE0000105264" 2017 . 28
"SE0000105264" 2018 . 28
"SE0000106205" 2013 43095 15
"SE0000106205" 2014 43630 15
"SE0000106205" 2015 44376 15
"SE0000106205" 2016 46337 15
"SE0000106205" 2017 50090 15
"SE0000106205" 2018 52233 15
"SE0000106270" 2013 128562 56
"SE0000106270" 2014 151419 56
"SE0000106270" 2015 180861 56
"SE0000106270" 2016 192267 56
"SE0000106270" 2017 200004 56
"SE0000106270" 2018 210400 56
"SE0000106320" 2013 6944 99
"SE0000106320" 2014 7581 99
"SE0000106320" 2015 7186 99
"SE0000106320" 2016 8344 99
"SE0000106320" 2017 9930 99
"SE0000106320" 2018 11785 99
"SE0000106759" 2013 . 73
"SE0000106759" 2014 . 73
"SE0000106759" 2015 . 73
"SE0000106759" 2016 . 73
"SE0000106759" 2017 . 73
"SE0000106759" 2018 . 73
"SE0000107419" 2013 62014 67
"SE0000107419" 2014 70565 67
"SE0000107419" 2015 41782 67
"SE0000107419" 2016 62190 67
end
[/CODE]
------------------ copy up to and including the previous line ------------------

Listed 100 out of 2202 observations
Use the count() option to list more

.