Dear Stata Users,
I am trying to calculate the sum of a variable but over/by more than one variable (company, department). As I show in the data example below, I would like to have the sum of the costs for each firm in each department. I know how to do this by just using the company identifies. I used this code but it did not give the company's department cost, rather it gave me the company's cost instead.
so for each firm (A, AA....) I need to calculate the total cost (cost) of production in each department (department_w).
----------------------- copy starting from the next line -----------------------
I would need the data to be structured like this example below:
Thanks for your support,
JLi
I am trying to calculate the sum of a variable but over/by more than one variable (company, department). As I show in the data example below, I would like to have the sum of the costs for each firm in each department. I know how to do this by just using the company identifies. I used this code but it did not give the company's department cost, rather it gave me the company's cost instead.
Code:
bysort company: egen sumvar = total(cost)
----------------------- copy starting from the next line -----------------------
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int year str2 company int department_w str2 est byte subdep_w int cost 2003 "A" 150 "B" 65 100 2004 "A" 150 "B" 70 121 2005 "A" 150 "B" 65 142 2006 "A" 150 "B" 70 163 2007 "A" 150 "B" 65 184 2008 "A" 150 "B" 70 205 2009 "A" 150 "B" 70 226 2010 "A" 150 "B" 65 247 2011 "A" 150 "B" 65 268 2012 "A" 150 "B" 70 289 2013 "A" 150 "B" 70 310 2014 "A" 150 "B" 65 331 2015 "A" 150 "B" 70 352 2016 "A" 150 "B" 65 373 2017 "A" 150 "B" 70 394 2018 "A" 150 "B" 65 415 2003 "A" 88 "B" 65 436 2004 "A" 88 "B" 70 457 2005 "A" 88 "B" 65 478 2006 "A" 88 "B" 70 499 2007 "A" 88 "B" 65 520 2008 "A" 88 "B" 70 541 2009 "A" 88 "B" 70 562 2010 "A" 88 "B" 65 583 2011 "A" 88 "B" 70 604 2012 "A" 88 "B" 65 625 2013 "A" 88 "B" 70 646 2014 "A" 88 "B" 65 667 2015 "A" 88 "B" 70 688 2016 "A" 88 "B" 65 709 2017 "A" 88 "B" 70 730 2018 "A" 88 "B" 65 751 2003 "AA" 150 "BB" 65 100 2004 "AA" 150 "BB" 70 121 2005 "AA" 150 "BB" 65 142 2006 "AA" 150 "BB" 70 163 2007 "AA" 150 "BB" 65 184 2008 "AA" 150 "BB" 70 205 2009 "AA" 150 "BB" 70 226 2010 "AA" 150 "BB" 65 247 2011 "AA" 150 "BB" 65 268 2012 "AA" 150 "BB" 70 289 2013 "AA" 150 "BB" 70 310 2014 "AA" 150 "BB" 65 331 2015 "AA" 150 "BB" 70 352 2016 "AA" 150 "BB" 65 373 2017 "AA" 150 "BB" 70 394 2018 "AA" 150 "BB" 65 415 2003 "AA" 88 "BB" 65 436 2004 "AA" 88 "BB" 70 457 2005 "AA" 88 "BB" 65 478 2006 "AA" 88 "BB" 70 499 2007 "AA" 88 "BB" 65 520 2008 "AA" 88 "BB" 70 541 2009 "AA" 88 "BB" 70 562 2010 "AA" 88 "BB" 65 583 2011 "AA" 88 "BB" 70 604 2012 "AA" 88 "BB" 65 625 2013 "AA" 88 "BB" 70 646 2014 "AA" 88 "BB" 65 667 2015 "AA" 88 "BB" 70 688 2016 "AA" 88 "BB" 65 709 2017 "AA" 88 "BB" 70 730 2018 "AA" 88 "BB" 65 751 end
Code:
year company department_w est subdep_w cost total 2003 A 150 B 65 100 4120 2004 A 150 B 70 121 4120 2005 A 150 B 65 142 4120 2006 A 150 B 70 163 4120 2007 A 150 B 65 184 4120 2008 A 150 B 70 205 4120 2009 A 150 B 70 226 4120 2010 A 150 B 65 247 4120 2011 A 150 B 65 268 4120 2012 A 150 B 70 289 4120 2013 A 150 B 70 310 4120 2014 A 150 B 65 331 4120 2015 A 150 B 70 352 4120 2016 A 150 B 65 373 4120 2017 A 150 B 70 394 4120 2018 A 150 B 65 415 4120 2003 A 88 B 65 436 9496 2004 A 88 B 70 457 9496 2005 A 88 B 65 478 9496 2006 A 88 B 70 499 9496 2007 A 88 B 65 520 9496 2008 A 88 B 70 541 9496 2009 A 88 B 70 562 9496 2010 A 88 B 65 583 9496 2011 A 88 B 70 604 9496 2012 A 88 B 65 625 9496 2013 A 88 B 70 646 9496 2014 A 88 B 65 667 9496 2015 A 88 B 70 688 9496 2016 A 88 B 65 709 9496 2017 A 88 B 70 730 9496 2018 A 88 B 65 751 9496 2003 AA 150 BB 65 100 3346 2004 AA 150 BB 70 121 3346 2005 AA 150 BB 65 142 3346 2006 AA 150 BB 70 163 3346 2007 AA 150 BB 65 184 3346 2008 AA 150 BB 70 56 3346 2009 AA 150 BB 70 300 3346 2010 AA 150 BB 65 321 3346 2011 AA 150 BB 65 342 3346 2012 AA 150 BB 70 363 3346 2013 AA 150 BB 70 384 3346 2014 AA 150 BB 65 132 3346 2015 AA 150 BB 70 153 3346 2016 AA 150 BB 65 174 3346 2017 AA 150 BB 70 195 3346 2018 AA 150 BB 65 216 3346 2003 AA 88 BB 65 237 6312 2004 AA 88 BB 70 258 6312 2005 AA 88 BB 65 279 6312 2006 AA 88 BB 70 300 6312 2007 AA 88 BB 65 321 6312 2008 AA 88 BB 70 342 6312 2009 AA 88 BB 70 363 6312 2010 AA 88 BB 65 384 6312 2011 AA 88 BB 70 405 6312 2012 AA 88 BB 65 426 6312 2013 AA 88 BB 70 447 6312 2014 AA 88 BB 65 468 6312 2015 AA 88 BB 70 489 6312 2016 AA 88 BB 65 510 6312 2017 AA 88 BB 70 531 6312 2018 AA 88 BB 65 552 6312
JLi
Comment