Hello everyone,
I am trying to use this code on this data example. The code works fine for all the estimation but the problem comes in saving the output. Right now in this example, i have three different samples and five different countries. I am using the matrix format and wants to save the output in such a way that for one sample and one country i will save coefficients in one txt file and standard errors in the other file. For example, under60_coef_Austria and the file will be under60_SE_Austria and in the columns of this file i want all the variables which you see in outcome loop. I have tried my best but not sure how to specify it. For example, in the end, i have put one code which i used right before closing all the loops. I will be glad if you can suggest something. Thanks
I am trying to put this code before closing all the loops in first code but it is not working.
I am trying to use this code on this data example. The code works fine for all the estimation but the problem comes in saving the output. Right now in this example, i have three different samples and five different countries. I am using the matrix format and wants to save the output in such a way that for one sample and one country i will save coefficients in one txt file and standard errors in the other file. For example, under60_coef_Austria and the file will be under60_SE_Austria and in the columns of this file i want all the variables which you see in outcome loop. I have tried my best but not sure how to specify it. For example, in the end, i have put one code which i used right before closing all the loops. I will be glad if you can suggest something. Thanks
Code:
* MAIN OUTCOMES FOR ALL SPECIFICATIONS FOR Under 60 Primary Sample local output "D:\New folder\Dropbox\Cross-country\Data\SH\Output" * Set programs location local programs "D:\New folder\Dropbox\Cross-country\Data\SH\Programs" local samples "under60 age60to64 over65" local spouses "freq" local fes "cohortXwave" local specs "freq" local outcomes "oop_spend age_hosp working ritearnsemp sitearnsemp hgovt hittot log_hittot" local countries "Austria Belgium Denmark France Germany" local range_freq 3 foreach samp in `samples' { foreach spouse in `spouses' { foreach fe in `fes' { foreach spec in `specs' { foreach country in `countries' { mat drop _all * Keep only those hospitalized *use "C:\Users\Jafri\Dropbox\Cross-country health risk\Data\HRS\Data\HRS_long.dta" if `samp'==1 & `spec'==1 & `spouse'==1, clear use "D:\New folder\Dropbox\Cross-country health risk\Data\SHARE\Data\SHARE_long.dta" if `samp'==1 & `spec'==1 & `spouse'==1 & `country'==1, clear * Generate event time dummies drop if evt_time<-`range_`spec'' drop if evt_time>`range_`spec'' forv i = 0/`range_`spec'' { if (`i' < 0) { local j = abs(`i') gen evt_f`j' = (evt_time == `i') } else { gen evt_l`i' = (evt_time == `i') } } egen cohortXwave = group(hacohort wave) * Define number of variables for "implied effects" matrix local J = 0 foreach outcome of varlist `outcomes' { local J = `J' + 1 } matrix define results_IEs = J(21,`J',.) local j = 1 foreach v of varlist `outcomes' { local controls "" if "`fe'"=="hhidpn" { xi i.wave drop _Iwave_11 local controls "_I*" if regexm("`v'","_c")==1 { drop _Iwave_2 _Iwave_3 } drop _Iwave_2 _Iwave_3 } di "FE = `fe', Sample = `samp', Spouse = `spouse', Spec = `spec', countr = `country' , Var = `v'" areg `v' evt_time evt_l* `controls' [pweight=rwtresp], absorb(`fe') cluster(hhidpn) *Saves N, number of individuals, and effective sample size to matrix local N = e(N) local C = e(N_clust) local R= e(r2) * Save first four rows as N, unique individuals, weighted individuals, and R-squared di "`N' \ `C' \ `R' " mat input N=(`N' \ `C' \ `R' ) mat rown N="N" "Indiv" "R2" * Save coefficients and add to column matrix eb = e(b) matrix eb = (N\ eb') * Save variance-covariance matrix matrix var= (e(V)) local colnames: coln var matrix list var // YU ADDED THIS local n=0 * Drop SE matrix from prior run cap mat drop se * Clean up matrices for output foreach col in `colnames' { local n=`n'+1 mat c`n'=var[`n'..., `n'] local rownames: rown c`n' foreach w of local rownames { local rw_c`n' `rw_c`n'' `w'_`col' } matrix rown c`n'= `rw_c`n'' matrix coln c`n'= `v' matrix se=(nullmat(se)\ c`n') cap mat drop c`n' local rw_c`n' "" } { mat se=(N\se) matrix results_ses_1=(nullmat(results_ses_1), se) matrix results_coefs_1 = (nullmat(results_coefs_1), eb) local col_list_1 `col_list_1' `v' } } // spec } // samples } // fes } // spouse } //country }
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double hhidpn byte hacohort float(oop_spend hhitothhhh_c) double hittot float(wave evt_time age_hosp under60 age60to64 over65 working hgovt ritearnsemp sitearnsemp log_hittot Austria Belgium Denmark France Germany) 1100032701 1 0 0 28222.52614044083 1 -1 54 1 0 0 100 0 14619.177 13574.95 10.247911 1 0 0 0 0 1100032701 1 684.8905 0 22797.06865355575 2 0 54 1 0 0 0 11740.98 0 10762.564 10.034431 1 0 0 0 0 1100032702 1 0 0 28246.117630832287 1 . . 0 0 0 100 0 13574.95 14619.177 10.248747 1 0 0 0 0 1100032702 1 45.00709 0 22797.06865355575 2 . . 0 0 0 100 11740.98 10762.564 0 10.034431 1 0 0 0 0 1100067401 3 . 0 8189.252254102425 4 . . 0 0 0 100 0 8181.979 0 9.0107 1 0 0 0 0 1100067401 3 101.79508 . 14244.525461033409 6 . . 0 0 0 0 14142.73 0 0 9.564198 1 0 0 0 0 1100067401 3 690.7431 0 13383.147826572063 5 . . 0 0 0 0 13296.805 0 0 9.501826 1 0 0 0 0 1100121501 3 718.5037 . 14251.311799985782 6 1 74 0 0 1 0 14251.312 0 0 9.564674 1 0 0 0 0 1100121501 3 . 0 13098.439884225993 4 -1 74 0 0 1 0 13091.167 0 0 9.480325 1 0 0 0 0 1100121501 3 1688.0035 0 13296.804937368373 5 0 74 0 0 1 0 13296.805 0 0 9.495355 1 0 0 0 0 1100149201 3 67.86339 . 28502.623599971565 6 0 63 0 1 0 0 23752.186 0 4750.4375 10.257787 1 0 0 0 0 1100149201 3 . 0 38182.571359359266 4 -2 63 0 1 0 0 0 0 38182.57 10.55016 1 0 0 0 0 1100149201 3 0 0 47488.5890620299 5 -1 63 0 1 0 0 0 0 47488.59 10.768266 1 0 0 0 0 1100149202 3 0 . 28502.623599971565 6 . . 0 0 0 0 23752.186 4750.4375 0 10.257787 1 0 0 0 0 1100149202 3 5180.573 0 47488.5890620299 5 . . 0 0 0 100 0 47488.59 0 10.768266 1 0 0 0 0 1100149202 3 . 0 38182.57045651663 4 . . 0 0 0 100 0 38182.57 0 10.55016 1 0 0 0 0 1100181601 1 104.4227 18168.209 10442.269253841407 1 . . 0 0 0 0 6265.361 2297.2993 1879.6085 9.253713 1 0 0 0 0 1100181602 1 0 18168.209 10442.269253841407 1 . . 0 0 0 100 6265.361 1879.6085 2297.2993 9.253713 1 0 0 0 0 1100181602 1 48.92075 0 . 2 . . 0 0 0 100 0 0 0 . 1 0 0 0 0 1100188101 3 0 . 9933.503641537709 6 0 85 0 0 1 0 9908.055 0 0 9.203769 1 0 0 0 0 1100188101 3 . 0 18727.641700101016 4 -2 85 0 0 1 0 18673.096 0 0 9.83781 1 0 0 0 0 1100188101 3 0 0 19107.68138077676 5 -1 85 0 0 1 0 19038.607 0 0 9.857898 1 0 0 0 0 1100188102 3 254.4877 . 16244.798867245698 6 0 90 0 0 1 0 16202.384 0 0 9.69559 1 0 0 0 0 1100188102 3 259.02866 0 19107.68138077676 5 -1 90 0 0 1 0 19038.607 0 0 9.857898 1 0 0 0 0 1100188102 3 . 0 18673.09517087742 4 -2 90 0 0 1 0 18673.096 0 0 9.834892 1 0 0 0 0 1100193701 3 . 0 4363.722337887615 4 0 51 1 0 0 0 4363.722 0 0 8.3813095 1 0 0 0 0 1100213201 1 782.732 0 12915.077520469353 2 . . 0 0 0 0 6457.539 0 0 9.4662285 1 0 0 0 0 1100213201 1 0 9906.882 11188.31493035444 1 . . 0 0 0 0 10024.578 1156.103 0 9.322715 1 0 0 0 0 1100213601 3 604.4002 0 59321.15772522945 5 -1 63 0 1 0 0 32896.64 12953.503 0 10.990738 1 0 0 0 0 1100213601 3 . 12060.362 43201.230444387395 4 -2 63 0 1 0 0 15927.587 27273.264 0 10.673647 1 0 0 0 0 1100213601 3 814.3607 . 50426.23315933544 6 0 63 0 1 0 0 35628.28 0 0 10.828287 1 0 0 0 0 1100213603 3 118.76093 . 50426.23315933544 6 2 58 1 0 0 0 35628.28 0 0 10.828287 1 0 0 0 0 1100213603 3 . 12060.362 43200.85114508739 4 0 58 1 0 0 0 15927.587 0 27273.264 10.673638 1 0 0 0 0 1100213603 3 43.17144 0 59321.15772522945 5 1 58 1 0 0 0 32896.64 0 12953.503 10.990738 1 0 0 0 0 1100218002 3 . 0 22909.54227390998 4 . . 0 0 0 0 22909.543 0 0 10.039352 1 0 0 0 0 1100218002 3 43.17144 0 24176.008977033405 5 . . 0 0 0 0 24176.01 0 0 10.093158 1 0 0 0 0 1100218003 3 . 0 22909.905917443557 4 -1 72 0 0 1 0 22909.543 0 0 10.03937 1 0 0 0 0 1100218003 3 0 0 24176.008977033405 5 0 72 0 0 1 0 24176.01 0 0 10.093158 1 0 0 0 0 1100235501 1 0 590 22455.63591735869 1 0 71 0 0 0 0 22429.994 0 0 10.019341 1 0 0 0 0 1100235502 1 0 590 22455.63591735869 1 0 71 0 0 0 0 22429.994 0 0 10.019341 1 0 0 0 0 1100252501 3 . 0 28573.290224959946 4 0 68 0 0 0 0 28573.29 0 0 10.260262 1 0 0 0 0 1100252501 3 0 0 28734.913526988275 5 1 68 0 0 0 0 28648.57 0 0 10.265903 1 0 0 0 0 1100252501 3 262.97064 . 27569.501994020113 6 2 68 0 0 0 0 27315.014 0 0 10.224502 1 0 0 0 0 1100252502 3 458.0779 . 27569.501994020113 6 2 68 0 0 0 0 27315.014 0 0 10.224502 1 0 0 0 0 1100252502 3 . 0 28718.74763622287 4 0 68 0 0 0 0 28573.29 0 0 10.26534 1 0 0 0 0 1100252502 3 388.543 0 28734.913526988275 5 1 68 0 0 0 0 28648.57 0 0 10.265903 1 0 0 0 0 1100257301 3 172.68578 0 25255.29509207954 5 . . 0 0 0 0 25255.295 0 0 10.13683 1 0 0 0 0 1100257301 3 . 0 25988.87567017261 4 . . 0 0 0 0 25982.33 0 0 10.165462 1 0 0 0 0 1100257302 3 172.68578 0 25255.29509207954 5 1 69 0 0 0 0 25255.295 0 0 10.13683 1 0 0 0 0 1100257302 3 . 0 25982.330086839174 4 0 69 0 0 0 0 25982.33 0 0 10.16521 1 0 0 0 0 1100280001 3 . 11639.608 23.1209056356435 4 . . 0 0 0 100 0 0 0 3.183079 1 0 0 0 0 1100280001 3 58.53217 . 53587.90078934619 6 . . 0 0 0 0 23005.69 30538.525 0 10.889097 1 0 0 0 0 1100280001 3 25.902866 0 18247.256967272468 5 . . 0 0 0 0 0 18132.006 0 9.811825 1 0 0 0 0 1100296502 3 0 . 7634.631321420955 6 2 87 0 0 0 0 7634.631 0 0 8.940581 1 0 0 0 0 1100296502 3 0 1200 26.70923854446815 5 1 87 0 0 0 0 0 0 0 3.321766 1 0 0 0 0 1100296502 3 . 0 7643.786961866473 4 0 87 0 0 0 0 7636.514 0 0 8.941779 1 0 0 0 0 1100319401 3 . 0 19654.932696902135 4 . . 0 0 0 0 19618.57 0 0 9.886134 1 0 0 0 0 1100319401 3 0 0 . 5 . . 0 0 0 0 18252.887 0 0 . 1 0 0 0 0 1100319401 3 25.44877 . . 6 . . 0 0 0 0 18187.389 0 0 . 1 0 0 0 0 1100319402 3 . 0 19618.568344086405 4 . . 0 0 0 0 19618.57 0 0 9.884283 1 0 0 0 0 1100321301 3 . 85 12000.781894504853 4 0 69 0 0 0 0 12000.236 0 0 9.392811 1 0 0 0 0 1100321301 3 0 0 12088.004488516703 5 1 69 0 0 0 0 12088.005 0 0 9.400052 1 0 0 0 0 1100321301 3 76.34631 . 12351.13689332101 6 2 69 0 0 0 0 12351.137 0 0 9.421584 1 0 0 0 0 1100368301 3 . 0 39396.048910150945 4 . . 0 0 0 100 0 18436.727 20954.96 10.581447 1 0 0 0 0 1100368302 3 . 0 39391.68518763966 4 . . 0 0 0 100 0 20954.96 18436.727 10.581335 1 0 0 0 0 1100423401 1 235.798 0 23100.377292294048 2 1 53 1 0 0 0 12328.028 0 10762.564 10.047647 1 0 0 0 0 1100423401 1 2401.722 800 . 1 0 53 1 0 0 0 12605.907 0 0 . 1 0 0 0 0 1100423401 1 . 0 26218.69838014142 4 3 53 1 0 0 0 13491.175 0 12727.523 10.174266 1 0 0 0 0 1100423402 1 . 0 26291.42708577288 4 . . 0 0 0 100 13491.175 12727.523 0 10.177036 1 0 0 0 0 1100423402 1 97.8415 0 23100.377292294048 2 . . 0 0 0 100 12328.028 10762.564 0 10.047647 1 0 0 0 0 1100437901 1 . . . 3 -1 76 0 0 1 0 . . . . 1 0 0 0 0 1100437901 1 156.63403 0 10024.57848368775 1 -3 76 0 0 1 0 10024.578 0 0 9.212895 1 0 0 0 0 1100437901 1 . 0 8181.979383539278 4 0 76 0 0 1 0 8181.979 0 0 9.009811 1 0 0 0 0 1100437901 1 0 0 6443.84095074327 2 -2 76 0 0 1 0 6434.057 0 0 8.771035 1 0 0 0 0 1100485501 1 626.5361 0 13815.122222832182 1 . . 0 0 0 0 13783.796 0 0 9.533591 1 0 0 0 0 1100485501 1 0 0 29352.448910157622 2 . . 0 0 0 0 28961.08 0 0 10.287166 1 0 0 0 0 1100485502 1 . . . 3 -1 78 0 0 1 0 . . . . 1 0 0 0 0 1100485502 1 . 0 8182.856558813008 4 0 78 0 0 1 0 8181.979 0 0 9.009919 1 0 0 0 0 1100485502 1 0 0 9116.898542004037 5 1 78 0 0 1 0 9107.448 0 0 9.117994 1 0 0 0 0 1100485502 1 187.96085 0 13783.795415070657 1 -3 78 0 0 1 0 13783.796 0 0 9.531322 1 0 0 0 0 1100485502 1 0 . 10570.917046599205 6 2 78 0 0 1 0 10566.33 0 0 9.265957 1 0 0 0 0 1100485502 1 146.76224 0 29352.448910157622 2 -2 78 0 0 1 0 28961.08 0 0 10.287166 1 0 0 0 0 1100497401 1 127.19395 0 15312.194181465558 2 . . 0 0 0 0 15263.273 0 0 9.63647 1 0 0 0 0 1100497401 1 26.10567 624.5474 36902.75519744703 1 . . 0 0 0 0 36892.313 0 0 10.516068 1 0 0 0 0 1100546001 1 176.1147 0 . 2 0 67 0 0 1 0 17807.152 12551.504 0 . 1 0 0 0 0 1100546001 1 . . . 3 1 67 0 0 1 0 . . . . 1 0 0 0 0 1100546001 1 73.09589 19600 . 1 -1 67 0 0 1 0 20024.096 1253.0723 0 . 1 0 0 0 0 1100596001 3 . 0 9.091088203932532 4 . . 0 0 0 0 0 0 0 2.3116527 1 0 0 0 0 1100678802 3 72.10485 . 35159.80092664269 6 . . 0 0 0 100 0 26127.404 8650.664 10.467688 1 0 0 0 0 1100678802 3 0 0 27239.493051228976 5 . . 0 0 0 100 0 21758.41 5437.914 10.21246 1 0 0 0 0 1100678804 3 0 . 35159.80092664269 6 . . 0 0 0 100 0 8650.664 26127.404 10.467688 1 0 0 0 0 1100678804 3 . 32000 . 4 . . 0 0 0 100 1090.9305 5000.099 0 . 1 0 0 0 0 1100678804 3 0 0 27239.493051228976 5 . . 0 0 0 100 0 5437.914 21758.41 10.21246 1 0 0 0 0 1100714401 3 169.6585 . 17353.36736194026 6 . . 0 0 0 0 16813.154 0 0 9.7616 1 0 0 0 0 1100714401 3 103.61147 0 17009.549173127074 5 . . 0 0 0 0 7770.86 0 0 9.741589 1 0 0 0 0 1100714401 3 . 0 19927.66534302011 4 . . 0 0 0 0 19909.48 0 0 9.899915 1 0 0 0 0 1100717001 3 215.85722 0 34278.127013865225 5 . . 0 0 0 0 33846.414 0 0 10.442292 1 0 0 0 0 1100717001 3 . 0 27273.264611797596 4 . . 0 0 0 0 27273.264 0 0 10.213698 1 0 0 0 0 1100717002 3 0 0 34278.127013865225 5 . . 0 0 0 0 33846.414 0 0 10.442292 1 0 0 0 0 1100717002 3 . 0 27273.26824823305 4 . . 0 0 0 0 27273.264 0 0 10.2137 1 0 0 0 0 end label values hacohort cohort label def cohort 1 "1.Original sample for country", modify label def cohort 3 "3.2010 Refreshment sample", modify
Code:
* Outputting and saving results local types = "coefs ses" foreach type of local types { drop _all mat coln results_`type'_1=`col_list_1' svmat2 results_`type'_1, names(col) rnames(var) order var outsheet using "`output'/SH_ES_`type'_`samp'_`spouse'_`spec'_`country'_FE`fe'.txt", replace } // end foreach type of local types
Comment