Hi. The loop below runs regressions on my data and saves the coefficients and standard errors for each regression. However, I want these regressions run separately on different groups in my dataset as identified by the variable cem_varlist (description below), with coefficients and standard errors for each group saved separately. The final result will be a separate coefficient dataset for each group of cem_varlist (so, save "${coeffdir}\coefficients_`groupnumber'.dta", replace. I tried using "levelsof cem_varlist, local(cemList)" but couldn't successfully incorporate it into the loop. Any help here will be much appreciated
Code:
//// PART 1. Define regression program //// capture program drop runEventStudies program define runEventStudies args data yList name use "${outdir}\\`data'.dta", clear * group indicator. So it's 1 both before and after the event - Luca mail 09/06 bysort state: egen treated = max(stepone) egen eventBlock = group(Treated_State adopt_week) qui gen post = weeknum > adopt_week //should turn on for the controls based on the state *for which the reg is run - aka eventblock *qui gen event = weeknum == adopt_week *qui gen treated_event = treated * event //specific to Luca's, n/a here qui gen treated_post = treated * post qui gen treated_trend = weeknum * treated qui sum eventBlock local N = r(max) disp "TOTAL: `N'" * Run regressions - include test rates foreach y of local yList { disp "`y'" forvalues i = 1/`N' { disp "`i'" * Balanced T, with FEs to control for week areg `y' post treated_post tested if eventBlock == `i' /// ,absorb(state) //mine is not a balanced panel - restrict analysis to make it balanced local b_`y'_`i'_b = _b[treated_post] local se_`y'_`i'_b = _se[treated_post] * Adding linear, group-specific trend qui areg `y' weeknum treated_trend post treated_post tested /// if eventBlock == `i',absorb(state) local b_`y'_`i'_b_trend = _b[treated_post] local se_`y'_`i'_b_trend = _se[treated_post] } } *** Step 2: Construct a dataset saving all coefficients and SEs *keep Treated_State adopt_week eventBlock cem_varlist qui duplicates drop foreach suff in b b_trend { foreach y of local yList { qui gen b_`y'_`suff' = "" qui gen se_`y'_`suff' = "" forvalues i = 1/`N' { qui replace b_`y'_`suff' = "`b_`y'_`i'_`suff''" if eventBlock == `i' qui replace se_`y'_`suff' = "`se_`y'_`i'_`suff''" if eventBlock == `i' } qui destring b_`y'_`suff', replace qui destring se_`y'_`suff', replace } } drop eventBlock * Save the data foreach x in Treated_State { encode `x', gen(temp) drop `x' rename temp `x' } * save *order state weeknum cem_varlist *sort state weeknum save "${coeffdir}\coefficients.dta", replace } end //////////// PART 2. Run the event studies //////////// * Acquired runEventStudies "test_matched_cohort" /// Data `"confirmed recovered deceased"' /// yList *Cem_varlist description --------------------------------------------------------------------------------------- cem_varlist (unlabeled) --------------------------------------------------------------------------------------- Type: Numeric (byte) Label: cem_varlist_lbl Range: [1,4] Units: 1 Unique values: 4 Missing .: 0/18,281 Tabulation: Freq. Numeric Label 11,433 1 Match on pop per sq km 3,239 2 Match on pop per sq km, %>60 2,024 3 Match on pop per sq km, %>60, healthexp per capita 1,585 4 Match on pop per sq km, %>60, total hospital beds *Data sample input str13 Treated_State byte cem_varlist str50 state byte(stepone weeknum adopt_week) float(confirmed recovered deceased tested) "Bihar" 1 "Andaman and Nicobar Islands" 0 3 22 .00025188917 0 0 0 "Bihar" 1 "Andaman and Nicobar Islands" 0 4 22 .0024469234 0 0 0 "Bihar" 1 "Andaman and Nicobar Islands" 0 5 22 .002626844 .0007196834 0 0 "Bihar" 1 "Andaman and Nicobar Islands" 0 6 22 .002806765 .00259086 0 .05048579 "Bihar" 1 "Andaman and Nicobar Islands" 0 7 22 .004713926 .002770781 0 .09640159 "Bihar" 1 "Andaman and Nicobar Islands" 0 8 22 .008312343 .003418496 0 .23756747 "Bihar" 1 "Andaman and Nicobar Islands" 0 9 22 .008312343 .007916517 0 0 "Bihar" 1 "Andaman and Nicobar Islands" 0 10 22 .008312343 .008312343 0 0 "Bihar" 1 "Andaman and Nicobar Islands" 0 11 22 .008312343 .008312343 0 1.2649873 "Bihar" 1 "Andaman and Nicobar Islands" 0 12 22 .008312343 .008312343 0 1.872976 "Bihar" 1 "Andaman and Nicobar Islands" 0 13 22 .008312343 .008312343 0 2.0354803 "Bihar" 1 "Andaman and Nicobar Islands" 0 14 22 .008780137 .008312343 0 2.461749 "Bihar" 1 "Andaman and Nicobar Islands" 0 15 22 .010759266 .0084562795 0 3.002123 "Bihar" 1 "Andaman and Nicobar Islands" 0 16 22 .013638 .009859662 0 3.484779 "Bihar" 1 "Andaman and Nicobar Islands" 0 17 22 .02400144 .011982728 0 3.956459 "Bihar" 1 "Andaman and Nicobar Islands" 0 18 22 .03573228 .01925153 0 4.38136 "Bihar" 1 "Andaman and Nicobar Islands" 0 19 22 .04303706 .03004678 0 4.748615 "Bihar" 1 "Andaman and Nicobar Islands" 0 20 22 .05505577 .03983447 0 5.203814 "Bihar" 1 "Andaman and Nicobar Islands" 0 21 22 .09913638 .04893847 .00043181 5.770313 "Bihar" 1 "Andaman and Nicobar Islands" 0 22 22 .23389708 .07607053 .003094638 6.429327 "Bihar" 1 "Andaman and Nicobar Islands" 0 23 22 .4445124 .19399065 .005325656 6.938575 "Bihar" 1 "Andaman and Nicobar Islands" 0 24 22 .6372796 .3602375 .00734077 7.283556 "Bihar" 1 "Andaman and Nicobar Islands" 0 25 22 .7401943 .5493343 .00942785 7.820691 "Bihar" 1 "Andaman and Nicobar Islands" 0 26 22 .7967974 .6804606 .011694854 8.805542 "Bihar" 1 "Andaman and Nicobar Islands" 0 27 22 .8549838 .7644117 .01270241 10.32965 "Bihar" 1 "Andaman and Nicobar Islands" 0 28 22 .9005398 .834041 .01302627 11.85135 "Bihar" 1 "Andaman and Nicobar Islands" 0 29 22 .9310543 .8765743 .013098237 13.269522 "Bihar" 1 "Andaman and Nicobar Islands" 0 30 22 .9619647 .904462 .013314142 14.64822 "Bihar" 1 "Andaman and Nicobar Islands" 0 31 22 .9869018 .9267722 .013602016 16.150414 "Bihar" 1 "Andaman and Nicobar Islands" 0 32 22 1.0160489 .9537243 .013889888 17.842175 "Bihar" 1 "Andaman and Nicobar Islands" 0 33 22 1.0441525 .9825477 .014213745 19.46225 "Bihar" 1 "Andaman and Nicobar Islands" 0 34 22 1.0762144 1.0124505 .01468154 21.295826 "Bihar" 1 "Andaman and Nicobar Islands" 0 35 22 1.1014034 1.0445844 .015077366 23.22494 "Bihar" 1 "Andaman and Nicobar Islands" 0 36 22 1.1281036 1.0713927 .015185318 25.49417 "Bihar" 1 "Andaman and Nicobar Islands" 0 37 22 1.1517812 1.0988845 .01536524 27.654156 "Bihar" 1 "Andaman and Nicobar Islands" 0 38 22 1.1746311 1.124973 .01536524 30.29748 "Bihar" 1 "Andaman and Nicobar Islands" 0 39 22 1.1883411 1.150126 .01536524 33.02638 "Bihar" 1 "Andaman and Nicobar Islands" 0 40 22 1.2028787 1.1686219 .01536524 35.946453 "Bihar" 1 "Andaman and Nicobar Islands" 0 41 22 1.2190716 1.1811444 .01536524 39.15775 "Bihar" 1 "Andaman and Nicobar Islands" 0 42 22 1.2323138 1.1967255 .01554516 42.51036 "Bihar" 1 "Andaman and Nicobar Islands" 0 43 22 1.2426412 1.211839 .015617128 45.15088 "Bihar" 1 "Andaman and Nicobar Islands" 0 44 22 1.2471393 1.222598 .015617128 47.19406 "Bihar" 1 "Andaman and Nicobar Islands" 0 45 22 1.2512414 1.2304786 .015617128 49.57629 "Bihar" 1 "Andaman and Nicobar Islands" 0 46 22 1.256423 1.234113 .015617128 52.06128 "Bihar" 1 "Andaman and Nicobar Islands" 0 47 22 1.2578626 1.238647 .015617128 54.43516 "Bihar" 1 "Andaman and Nicobar Islands" 0 48 22 .3594099 .35465994 .0044620363 16.013422 "Bihar" 1 "Andhra Pradesh" 0 1 22 5.471253e-07 0 0 0 "Bihar" 1 "Andhra Pradesh" 0 2 22 3.009189e-06 0 0 0 "Bihar" 1 "Andhra Pradesh" 0 3 22 .000016413758 0 0 0 "Bihar" 1 "Andhra Pradesh" 0 4 22 .00014526176 2.462064e-06 2.7356265e-07 .0014375716 "Bihar" 1 "Andhra Pradesh" 0 5 22 .0005900746 .000011763193 6.839066e-06 .0017436882 "Bihar" 1 "Andhra Pradesh" 0 6 22 .0009243682 .00003419533 .000019970073 .02332368 "Bihar" 1 "Andhra Pradesh" 0 7 22 .0014745026 .0001917674 .00004240221 .07079199 "Bihar" 1 "Andhra Pradesh" 0 8 22 .002392852 .0005214104 .00005991022 .15572937 "Bihar" 1 "Andhra Pradesh" 0 9 22 .003275092 .0012017606 .00006784353 .2542428 "Bihar" 1 "Andhra Pradesh" 0 10 22 .0040120697 .0020385887 .00008836073 .3673837 "Bihar" 1 "Andhra Pradesh" 0 11 22 .004784063 .003031621 .00009930324 .4304929 "Bihar" 1 "Andhra Pradesh" 0 12 22 .005759861 .003819208 .00010997218 .6190895 "Bihar" 1 "Andhra Pradesh" 0 13 22 .007340232 .0046437257 .00012638593 .753984 "Bihar" 1 "Andhra Pradesh" 0 14 22 .00960916 .005391099 .0001471767 .922762 "Bihar" 1 "Andhra Pradesh" 0 15 22 .013052767 .006761648 .0001690617 1.1162204 "Bihar" 1 "Andhra Pradesh" 0 16 22 .01895543 .008865618 .0002319811 1.3809762 "Bihar" 1 "Andhra Pradesh" 0 17 22 .02794497 .01260139 .0003528958 1.7078408 "Bihar" 1 "Andhra Pradesh" 0 18 22 .0407901 .019571764 .0004853001 2.0204346 "Bihar" 1 "Andhra Pradesh" 0 19 22 .06419585 .0333213 .000790049 2.2918196 "Bihar" 1 "Andhra Pradesh" 0 20 22 .1162458 .05582647 .0014559004 2.682829 "Bihar" 1 "Andhra Pradesh" 0 21 22 .21597633 .10149994 .0022177722 3.374867 "Bihar" 1 "Andhra Pradesh" 0 22 22 .3397927 .18642883 .0030907106 4.1749 "Bihar" 1 "Andhra Pradesh" 0 23 22 .4695284 .2959302 .0042246277 4.9658 "Bihar" 1 "Andhra Pradesh" 0 24 22 .58839357 .4172916 .005406418 5.680329 "Bihar" 1 "Andhra Pradesh" 0 25 22 .7142318 .53221035 .00661666 6.433842 "Bihar" 1 "Andhra Pradesh" 0 26 22 .8525875 .6513978 .007742643 7.246796 "Bihar" 1 "Andhra Pradesh" 0 27 22 .9908242 .7944073 .008733761 8.121592 "Bihar" 1 "Andhra Pradesh" 0 28 22 1.1181855 .9336517 .00965594 9.074339 "Bihar" 1 "Andhra Pradesh" 0 29 22 1.2246617 1.0736066 .01045064 10.027867 "Bihar" 1 "Andhra Pradesh" 0 30 22 1.316779 1.1890486 .011077371 10.985807 "Bihar" 1 "Andhra Pradesh" 0 31 22 1.3962908 1.2869414 .011589754 11.90972 "Bihar" 1 "Andhra Pradesh" 0 32 22 1.4617015 1.367466 .012043048 12.83992 "Bihar" 1 "Andhra Pradesh" 0 33 22 1.5124685 1.434849 .012404697 13.795343 "Bihar" 1 "Andhra Pradesh" 0 34 22 1.5551533 1.4886323 .01268291 14.765497 "Bihar" 1 "Andhra Pradesh" 0 35 22 1.59089 1.5348885 .012889724 15.83183 "Bihar" 1 "Andhra Pradesh" 0 36 22 1.6203884 1.5671088 .01304839 16.8499 "Bihar" 1 "Andhra Pradesh" 0 37 22 1.6397145 1.5933694 .013191737 17.763556 "Bihar" 1 "Andhra Pradesh" 0 38 22 1.6541574 1.6154376 .013316208 18.630775 "Bihar" 1 "Andhra Pradesh" 0 39 22 1.663732 1.6351947 .013402928 19.378273 "Bihar" 1 "Andhra Pradesh" 0 40 22 1.671569 1.647457 .01348062 20.14241 "Bihar" 1 "Andhra Pradesh" 0 41 22 1.6782236 1.6557584 .01352439 20.95833 "Bihar" 1 "Andhra Pradesh" 0 42 22 1.6839167 1.6626638 .013561868 21.758425 "Bihar" 1 "Andhra Pradesh" 0 43 22 1.6882893 1.6681576 .013597157 22.44178 "Bihar" 1 "Andhra Pradesh" 0 44 22 1.6920577 1.6727334 .013635456 23.10391 "Bihar" 1 "Andhra Pradesh" 0 45 22 1.695169 1.6768777 .01365953 23.742603 "Bihar" 1 "Andhra Pradesh" 0 46 22 1.697125 1.6801927 .013675943 24.232656 "Bihar" 1 "Andhra Pradesh" 0 47 22 1.6989805 1.682627 .013691536 24.783926 "Bihar" 1 "Andhra Pradesh" 0 48 22 .4857258 .48110995 .003913587 7.176597 "Bihar" 1 "Arunachal Pradesh" 0 4 22 .00001899696 0 0 0 "Bihar" 1 "Arunachal Pradesh" 0 5 22 .00006648936 0 0 0 "Bihar" 1 "Arunachal Pradesh" 0 6 22 .00006648936 .00002849544 0 .0080642095 "Bihar" 1 "Arunachal Pradesh" 0 7 22 .00006648936 .00006648936 0 .0218845 "Bihar" 1 "Arunachal Pradesh" 0 8 22 .00006648936 .00006648936 0 .03649316 "Bihar" 1 "Arunachal Pradesh" 0 9 22 .00006648936 .00006648936 0 .0587576
Comment