I am trying to create a LateX table that summarizes statistics and differences for a number of variables (e.g., in the following dataset turnout, share_m5s, share_incum etc..) between treatment and control group in pre and post periods. (Treatment is defined by having pop2011<5000 and Post by having year>2013).
I generated the necssary dummies with the code:
gen pop_5000 = pop2011-5000
gen TREATED = (pop_5000<0)
gen POST = (year>2013)
Now I was trying to implement the aformentioned table, and my idea was that of using an approach like the following one (not complete):
forval i=0/1 {
forval j=0/1{
foreach x in turnout share_m5s share_lega_fdi share_incum{
sum `x' if TREATED==`i' & POST==`j'
scalar mean_`x'_`i'_`j' = round(r(mean), 0.01)
scalar sd_`x'_`i'_`j' = round(r(sd), 0.01)
scalar count_`x'_`i'_`j' = r(N)
}
}
}
** Store results in a matrix
mat diff = J(12,6,.)
local p = 1
foreach var in turnout share_m5s share_lega_fdi share_incum {
foreach v in mean sd count{
forval i = 0/1 {
mat diff[`p',`i'+1] = `v'_`var'_`i'_0
local p = `p' + 1
}
}
Is there a faster and more intuitive way of carrying out this task?
Code:
* Example generated by -dataex-. For more info, type help dataex clear input long n_istat int year byte election float(turnout share_m5s) long(pop2011 population) float(citizenship cohabiting) double fam_comp float(share_lega_fdi share_incum sh_immigr sh_female) 1001 2013 1 74.096664 25.33602 2644 2669 0 4 2.09 7.594086 49.93279 8.542525 52.52904 1001 2014 2 76.86364 18.656717 2644 2708 2 4 2.08 15.671642 59.97286 8.825702 52.51108 1002 2013 1 80.61644 36.424133 3819 3842 8 0 2.52 3.549246 42.81277 6.923477 49.24519 1002 2014 2 68.49229 30.46523 3819 3806 10 0 2.49 8.054028 55.67784 7.094062 49.23804 1004 2013 1 75.71529 27.03739 1791 1800 0 1 2.27 7.286673 49.85619 7 50.83333 1004 2014 2 60.44177 18.727915 1791 1785 1 1 2.22 11.425206 65.01767 8.571428 51.37255 1006 2013 1 81.42913 41.04089 6303 6377 1 1 2.22 5.179678 33.3829 4.876901 51.02713 1006 2014 2 78.40909 33.271767 6303 6406 4 1 2.22 9.155672 47.44063 4.527006 51.07711 1009 2013 1 85.02604 34.48 1966 1987 2 0 2.37 10.24 37.04 7.549069 50.02516 1009 2014 2 80.52869 24.09532 1966 2001 0 0 2.35 16.681377 51.98588 8.145927 49.97501 1012 2013 1 83.95785 31.22302 1039 1080 0 0 2.52 7.482015 45.75539 4.259259 48.24074 1012 2014 2 80.37166 23.704866 1039 1072 1 0 2.43 13.814756 55.25903 4.7574625 47.94776 1014 2013 1 77.68199 30.27167 1347 1344 4 1 2.29 9.184994 43.20828 7.514881 51.26488 1014 2014 2 62.34742 25.51834 1347 1325 2 1 2.31 13.23764 55.02393 7.698113 50.56604 1016 2013 1 77.16475 28.248587 3161 3219 1 1 2.31 8.885465 46.07088 4.25598 51.50668 1016 2014 2 64.67958 23.83826 3161 3187 1 1 2.3 10.500905 59.80688 4.267336 51.86696 1018 2013 1 82.36868 24.136496 3783 3783 1 0 2.34 7.657095 40.6159 3.6743326 50.09252 1018 2014 2 71.36274 20.10737 3783 3793 0 0 2.34 10.444118 60.2245 3.69101 50.27682 1020 2013 1 76.03207 28.16594 3280 3293 9 3 2.03 3.7117906 47.92577 13.027635 52.53568 1020 2014 2 67.62534 19.87842 3280 3281 6 2 2.02 8.8145895 63.769 12.740018 52.60591 1021 2013 1 74.26471 26.51593 1623 1648 4 0 2.1 9.969168 45.11819 4.550971 50.6068 1021 2014 2 70.46444 21.239954 1623 1629 1 0 2.1 18.254879 55.79794 4.358502 50.33763 1022 2013 1 76.26783 24.115755 3212 3226 2 5 1.86 10.557343 46.0343 10.136392 50.71296 1022 2014 2 59.9374 15.526676 3212 3248 1 5 1.85 15.18468 63.88509 10.868227 50.5234 1025 2013 1 77.80365 27.76323 3376 3426 8 3 2.32 10.214772 45.10215 10.12843 50.84647 1025 2014 2 55.24166 20.5803 3376 3488 2 3 2.31 13.562754 60.25641 10.091743 50.48738 1027 2013 1 79.80324 23.73272 2112 2107 0 0 2.18 8.90937 47.31183 4.6511626 49.97627 1027 2014 2 60.74197 18.463074 2112 2115 0 0 2.2 14.371258 60.57884 4.6335697 49.55083 1030 2013 1 76.77789 28.28887 3643 3762 9 0 2.19 8.325666 45.21619 6.937799 51.80755 1030 2014 2 73.76351 20.76848 3643 3736 15 0 2.21 14.980545 57.68482 7.334047 51.71306 1032 2013 1 75.74932 42.93194 2320 2272 4 0 2.24 4.86163 35.676888 7.526409 49.77993 1032 2014 2 73.00482 35.051548 2320 2292 5 0 2.21 7.375099 49.08803 7.635253 49.34555 1033 2013 1 77.25971 30.832874 3056 3107 1 1 2.41 7.335907 44.62217 5.922112 50.62762 1033 2014 2 77.43468 25.09782 3056 3103 3 1 2.39 14.086082 55.72945 5.929745 50.56397 1034 2013 1 77.96131 33.793648 8402 8530 15 1 2.36 4.142829 44.42691 5.498241 50.98476 1034 2014 2 73.76639 28.270775 8402 8600 5 1 2.36 8.418312 57.95183 5.627907 50.96511 1035 2013 1 78.622 30.371975 4517 4559 3 2 2.27 9.209101 40.9173 5.11077 50.33999 1035 2014 2 68.07748 24.328533 4517 4622 12 2 2.27 11.677696 56.24757 5.300735 50.45435 1038 2013 1 85.63338 32.850243 8479 8552 9 0 2.48 5.296117 44.74861 3.086997 50.22217 1038 2014 2 76.29956 26.943645 8479 8584 5 0 2.47 7.754011 58.51501 3.145387 50.25629 1039 2013 1 74.74074 35.444332 1726 1701 6 3 2.25 5.924413 43.00306 7.701352 51.38154 1039 2014 2 58.11481 27.984085 1726 1689 15 3 2.25 12.997348 53.71353 7.400829 51.8058 1040 2013 1 80.61143 45.65884 1545 1564 0 0 2.28 4.085802 35.64862 4.987212 50.8312 1040 2014 2 78.00797 37.142857 1545 1548 0 0 2.3 8.461538 43.62637 4.5219636 51.03359 1041 2013 1 81.1943 27.33564 1405 1426 3 2 2.62 9.11188 43.13725 4.4880786 50.77139 1041 2014 2 61.77606 21.94093 1405 1438 0 2 2.6 13.220816 59.91561 4.659249 50.69541 1042 2013 1 78.551 25.252525 1228 1235 0 1 2.25 9.217172 45.20202 3.643725 53.19838 1042 2014 2 77.09708 17.060368 1228 1247 0 1 2.2 11.417322 63.51706 4.490778 52.92702 1043 2013 1 79.74173 29.22423 1588 1608 3 0 2.44 11.158342 43.03932 6.654229 49.75124 1043 2014 2 83.29375 27.89189 1588 1650 5 0 2.45 16.756756 51.35135 6.909091 49.39394 1044 2013 1 76.94604 46.25233 6363 6313 19 0 2.18 4.5612164 32.782074 8.522098 52.47901 1044 2014 2 74.05734 36.910847 6363 6310 27 0 2.16 7.524134 41.59568 8.779715 52.07607 1045 2013 1 83.75168 31.467545 6386 6417 7 3 2.32 4.4684854 41.50988 4.0984883 50.78697 1045 2014 2 77.30026 26.15225 6386 6411 2 3 2.31 9.347488 56.9653 4.149119 50.75651 1046 2013 1 75.3971 31.24697 3511 3509 0 1 2.24 9.267345 40.46579 5.756626 50.92619 1046 2014 2 74.66205 23.930754 3511 3506 3 1 2.25 14.562118 56.26273 5.932687 50.94124 1047 2013 1 77.20831 26.91626 7483 7566 6 2 2.17 6.801063 46.89854 6.92572 52.52445 1047 2014 2 68.31923 20.43269 7483 7620 6 3 2.17 12.5 60.36057 7.742782 52.63779 1048 2013 1 83.66899 30.05923 6215 6183 2 3 2.55 5.231984 43.63277 3.299369 51.28578 1048 2014 2 69.834145 23.29982 6215 6204 4 3 2.53 8.279125 63.21703 3.4493876 51.2089 1049 2013 1 78.5265 26.90909 1382 1376 1 0 2.23 13.454546 40.84849 3.77907 50.43605 1049 2014 2 73.640854 21.57623 1382 1364 3 0 2.24 18.475452 53.48837 3.812317 49.56012 1050 2013 1 77.23258 21.983913 1286 1271 0 0 2.3 8.981234 50.67024 7.946499 51.45555 1050 2014 2 72.284004 18.156809 1286 1302 0 0 2.27 14.580467 62.86107 8.371736 52.07373 1051 2013 1 84.55446 36.074413 5566 5606 5 3 2.43 4.232947 40.33432 2.675705 51.12379 1051 2014 2 79.23958 30.05257 5566 5679 5 3 2.44 6.921729 54.08878 2.92305 50.74837 1053 2013 1 82.09393 30.593325 2527 2551 5 2 2.4 6.551298 41.10012 4.351235 51.35241 1053 2014 2 71.19266 23.16305 2527 2574 0 3 2.38 11.826452 57.73268 4.312354 51.7094 1055 2013 1 81.19907 41.68514 2116 2113 2 0 2.19 4.28677 32.446415 2.886891 50.30762 1055 2014 2 79.41177 30.933544 2116 2092 0 0 2.18 9.25633 44.38291 2.963671 50.33461 1058 2013 1 79.16725 27.169035 9156 9181 8 4 2.29 7.666417 43.02543 7.831391 51.32339 1058 2014 2 61.54739 20.567696 9156 9210 12 5 2.29 10.633026 60.64429 8.013029 51.41151 1060 2013 1 75.83062 31.95691 1820 1880 7 2 2.19 6.193896 43.35727 5.478724 51.17021 1060 2014 2 64.915695 22.82958 1820 1926 1 2 2.21 11.682743 56.05573 5.088266 51.34995 1061 2013 1 78.82653 24.26966 1491 1474 0 0 2.25 4.1573033 50.11236 4.477612 51.69606 1061 2014 2 67.967476 17.424242 1491 1499 1 0 2.25 7.19697 63.76263 4.736491 51.16745 1062 2013 1 83.44624 36.743214 2931 2995 2 3 2.45 5.897704 40.44885 4.5742903 49.91653 1062 2014 2 80.87386 32.010868 2931 3008 0 2 2.45 8.75 52.22826 4.7207446 49.80053 1064 2013 1 80.04308 26.031164 1791 1766 1 5 2.38 7.699358 41.42988 5.719139 50.2265 1064 2014 2 75.6393 22.35849 1791 1769 4 5 2.41 11.415094 54.81132 5.822499 50.53703 1065 2013 1 81.36793 32.649963 2193 2224 1 1 2.45 8.4282465 39.78739 7.733813 51.21403 1065 2014 2 71.95248 24.761906 2193 2256 1 1 2.44 14.84127 55.15873 8.067376 50.53191 1066 2013 1 74.28347 24.617653 9917 9903 7 5 2.24 6.762484 51.98083 9.118449 52.29728 1066 2014 2 58.84154 18.219557 9917 9969 3 5 2.24 13.16882 62.93819 9.860568 51.90089 1068 2013 1 84.38544 28.1892 6363 6342 2 2 2.36 6.856187 42.88103 3.7212236 50.93031 1068 2014 2 76.7809 22.799156 6363 6380 2 2 2.37 12.59884 57.27464 3.7774296 50.67398 1069 2013 1 78.37123 30.21633 2309 2258 1 1 2.23 5.303559 47.73203 4.871568 51.28432 1069 2014 2 62.04039 25.15611 2309 2264 6 1 2.26 10.258698 59.58965 4.770318 51.19258 1070 2013 1 77.45274 24.15267 5568 5595 3 4 2.31 15.23664 42.07633 6.16622 50.22342 1070 2014 2 68.126945 18.837357 5568 5582 2 4 2.3 18.329098 57.46506 6.270154 50.59119 1071 2013 1 79.31035 29.17821 1857 1849 2 2 2.29 5.817174 45.15236 5.732829 50.29746 1071 2014 2 66.97995 24.500526 1857 1831 0 2 2.32 12.40799 58.78023 4.969962 50.7373 1072 2013 1 71.2156 22.635136 1056 1052 0 1 1.79 17.736486 40.87838 6.939164 49.71483 1072 2014 2 71.46145 16.724138 1056 1062 1 1 1.79 10.172414 66.72414 8.757062 50.28249 1074 2013 1 78.98734 27.07641 1007 989 0 1 2 16.943521 35.714287 10.81901 47.42164 1074 2014 2 81.35169 19.072165 1007 998 0 0 1.97 18.213058 56.35739 11.322645 46.29258 1076 2013 1 82.38268 45.54637 1700 1712 4 0 2.23 5.325987 30.30303 4.5560746 50.29206 1076 2014 2 79.74138 35.96838 1700 1707 0 0 2.24 10.77075 41.0079 4.745167 49.85354 1077 2013 1 79.27823 30.08378 2106 2118 0 1 2.31 6.549886 43.56436 4.3437204 52.03022 1077 2014 2 75.351715 20.77607 2106 2121 0 0 2.31 10.99434 58.36702 4.620462 52.09807 end label values election electionlab label def electionlab 1 "Parliamentary", modify label def electionlab 2 "European", modify
I generated the necssary dummies with the code:
gen pop_5000 = pop2011-5000
gen TREATED = (pop_5000<0)
gen POST = (year>2013)
Now I was trying to implement the aformentioned table, and my idea was that of using an approach like the following one (not complete):
forval i=0/1 {
forval j=0/1{
foreach x in turnout share_m5s share_lega_fdi share_incum{
sum `x' if TREATED==`i' & POST==`j'
scalar mean_`x'_`i'_`j' = round(r(mean), 0.01)
scalar sd_`x'_`i'_`j' = round(r(sd), 0.01)
scalar count_`x'_`i'_`j' = r(N)
}
}
}
** Store results in a matrix
mat diff = J(12,6,.)
local p = 1
foreach var in turnout share_m5s share_lega_fdi share_incum {
foreach v in mean sd count{
forval i = 0/1 {
mat diff[`p',`i'+1] = `v'_`var'_`i'_0
local p = `p' + 1
}
}
Is there a faster and more intuitive way of carrying out this task?