Dear Stata users,
I have this data example that consist of firm-level data. My first step is to calulate the var "dem" (demand).
I use this code
to obtain this data
In this example I pasted only one firm, but in fact I have about 1250 firms. I need to generate another variable: tot_dem (total demand) by summing over the sec variable.
My question is if I want to do this which command/code is right? The "sum" or the "total". Both of them give me the same result. I checked the description and doesnt show that sum and total commands are the same.
Below is the code and the final result:
then
Thanks
JLi
I have this data example that consist of firm-level data. My first step is to calulate the var "dem" (demand).
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int year str4 comp byte sec int total_a float perc_a long cost 2009 "A125" 1 4652 1.163 2500549 2009 "A125" 1 4787 1.197 2501240 2009 "A125" 1 4922 1.231 2501931 2009 "A125" 1 5057 1.264 2502622 2009 "A125" 1 5192 1.298 2503313 2009 "A125" 1 5327 1.332 2504004 2009 "A125" 1 5462 1.366 2504695 2009 "A125" 1 5597 1.399 2505386 2009 "A125" 1 5732 1.433 2506077 2009 "A125" 1 5867 1.467 2506768 2010 "A125" 1 6002 1.501 2507459 2010 "A125" 1 6137 1.534 2508150 2010 "A125" 1 6272 1.568 2508841 2010 "A125" 1 6407 1.602 2509532 2010 "A125" 1 6542 1.636 2510223 2010 "A125" 1 6677 1.669 2510914 2010 "A125" 1 6812 1.703 2511605 2010 "A125" 1 6947 1.737 2512296 2010 "A125" 1 7082 1.771 2512987 2010 "A125" 1 7217 1.804 2513678 2011 "A125" 1 7352 1.838 2514369 2011 "A125" 1 7487 1.872 2515060 2011 "A125" 1 7622 1.906 2515751 2011 "A125" 1 7757 1.939 2516442 2011 "A125" 1 7892 1.973 2517133 2011 "A125" 1 8027 2.007 2517824 2011 "A125" 1 8162 2.041 2518515 2011 "A125" 1 8297 2.074 2519206 2011 "A125" 1 8432 2.108 2519897 2011 "A125" 1 8567 2.142 2520588 end
I use this code
Code:
gen dem=perc_a * cost
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int year str4 comp byte sec int total_a float perc_a long cost float dem 2009 "A125" 1 4652 1.163 2500549 2908138.5 2009 "A125" 1 4787 1.197 2501240 2993984 2009 "A125" 1 4922 1.231 2501931 3079877 2009 "A125" 1 5057 1.264 2502622 3163314 2009 "A125" 1 5192 1.298 2503313 3249300 2009 "A125" 1 5327 1.332 2504004 3335333 2009 "A125" 1 5462 1.366 2504695 3421413.5 2009 "A125" 1 5597 1.399 2505386 3505035 2009 "A125" 1 5732 1.433 2506077 3591208 2009 "A125" 1 5867 1.467 2506768 3677429 2010 "A125" 1 6002 1.501 2507459 3763696 2010 "A125" 1 6137 1.534 2508150 3847502 2010 "A125" 1 6272 1.568 2508841 3933862.5 2010 "A125" 1 6407 1.602 2509532 4020270 2010 "A125" 1 6542 1.636 2510223 4106725 2010 "A125" 1 6677 1.669 2510914 4190715.5 2010 "A125" 1 6812 1.703 2511605 4277263 2010 "A125" 1 6947 1.737 2512296 4363858 2010 "A125" 1 7082 1.771 2512987 4450500 2010 "A125" 1 7217 1.804 2513678 4534675 2011 "A125" 1 7352 1.838 2514369 4621410.5 2011 "A125" 1 7487 1.872 2515060 4708192.5 2011 "A125" 1 7622 1.906 2515751 4795021.5 2011 "A125" 1 7757 1.939 2516442 4879381 2011 "A125" 1 7892 1.973 2517133 4966303.5 2011 "A125" 1 8027 2.007 2517824 5053273 2011 "A125" 1 8162 2.041 2518515 5140289 2011 "A125" 1 8297 2.074 2519206 5224833 2011 "A125" 1 8432 2.108 2519897 5311943 2011 "A125" 1 8567 2.142 2520588 5399100 end
My question is if I want to do this which command/code is right? The "sum" or the "total". Both of them give me the same result. I checked the description and doesnt show that sum and total commands are the same.
Below is the code and the final result:
Code:
bysort sec year: egen tot_dem1= total(dem) bysort sec year: egen tot_dem2= sum(dem)
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int year str4 comp byte sec int total_a float perc_a long cost float(dem tot_dem1 tot_dem2) 2009 "A125" 1 4652 1.163 2500549 2908138.5 32925034 32925034 2009 "A125" 1 4787 1.197 2501240 2993984 32925034 32925034 2009 "A125" 1 4922 1.231 2501931 3079877 32925034 32925034 2009 "A125" 1 5057 1.264 2502622 3163314 32925034 32925034 2009 "A125" 1 5192 1.298 2503313 3249300 32925034 32925034 2009 "A125" 1 5327 1.332 2504004 3335333 32925034 32925034 2009 "A125" 1 5462 1.366 2504695 3421413.5 32925034 32925034 2009 "A125" 1 5597 1.399 2505386 3505035 32925034 32925034 2009 "A125" 1 5732 1.433 2506077 3591208 32925034 32925034 2009 "A125" 1 5867 1.467 2506768 3677429 32925034 32925034 2010 "A125" 1 6002 1.501 2507459 3763696 41489068 41489068 2010 "A125" 1 6137 1.534 2508150 3847502 41489068 41489068 2010 "A125" 1 6272 1.568 2508841 3933862.5 41489068 41489068 2010 "A125" 1 6407 1.602 2509532 4020270 41489068 41489068 2010 "A125" 1 6542 1.636 2510223 4106725 41489068 41489068 2010 "A125" 1 6677 1.669 2510914 4190715.5 41489068 41489068 2010 "A125" 1 6812 1.703 2511605 4277263 41489068 41489068 2010 "A125" 1 6947 1.737 2512296 4363858 41489068 41489068 2010 "A125" 1 7082 1.771 2512987 4450500 41489068 41489068 2010 "A125" 1 7217 1.804 2513678 4534675 41489068 41489068 2011 "A125" 1 7352 1.838 2514369 4621410.5 50099744 50099744 2011 "A125" 1 7487 1.872 2515060 4708192.5 50099744 50099744 2011 "A125" 1 7622 1.906 2515751 4795021.5 50099744 50099744 2011 "A125" 1 7757 1.939 2516442 4879381 50099744 50099744 2011 "A125" 1 7892 1.973 2517133 4966303.5 50099744 50099744 2011 "A125" 1 8027 2.007 2517824 5053273 50099744 50099744 2011 "A125" 1 8162 2.041 2518515 5140289 50099744 50099744 2011 "A125" 1 8297 2.074 2519206 5224833 50099744 50099744 2011 "A125" 1 8432 2.108 2519897 5311943 50099744 50099744 2011 "A125" 1 8567 2.142 2520588 5399100 50099744 50099744 end
then
Thanks
JLi

Comment