Hi everyone,
I recently came across with the -runby- command which I find extremely helpful.
I have a cross-section of countries, regions and a number of variables reflecting different aspects of institutional quality as well as a population weight for each region. Example of how the data looks:
* Example generated by -dataex-. For more info, type help dataex
clear
input str10 iso3 str7 TL2 str8 TL3 float dem_pop_weight double eqi
"GRC" "EL30" "EL301" .3556652 -1.0455000400543213
"GRC" "EL30" "EL302" .3556652 -1.0455000400543213
"GRC" "EL30" "EL303" .3556652 -1.0455000400543213
"GRC" "EL30" "EL304" .3556652 -1.0455000400543213
"GRC" "EL30" "EL305" .3556652 -1.0455000400543213
"GRC" "EL30" "EL306" .3556652 -1.0455000400543213
"GRC" "EL30" "EL307" .3556652 -1.0455000400543213
"GRC" "EL41" "EL411" .018593332 -.9587500095367432
"GRC" "EL41" "EL412" .018593332 -.9587500095367432
"GRC" "EL41" "EL413" .018593332 -.9587500095367432
"GRC" "EL42" "EL421" .030548707 -.9900000095367432
"GRC" "EL42" "EL422" .030548707 -.9900000095367432
*
I want to create p90/p10 ratio for each institution for each country. I am using the following code which works fine:
capture program drop my_summarize
program define my_summarize
local statistics N mean p10 p90
summ eqi [aw=dem_pop_weight], detail
foreach s of local statistics {
gen `s' = r(`s') in 1
}
// keep in 1
// keep iso3 `statistics'
exit
end
runby my_summarize, by(iso3) verbose
exit
//////////
(Variables: eqi: institutional quality variable, dem_pop_weight is a population weight for each region and iso3: country)
I tried creating a loop to run the same code for all institutions. The code starts running and produces summary tables but as the loop runs I am getting the following error: invalid syntax. Also this is mentioned at the end
Number of by-groups = 42
by-groups with errors = 42
The code is the following:
capture program drop my_summarize
program define my_summarize
local statistics N mean p10 p90
foreach xvar of varlist $list_eqi {
global xvar `xvar'
foreach s of local statistics {
summarize ${xvar}[aw=dem_pop_weight], detail
gen `s'_${xvar} = r(`s')_${xvar}
}
}
exit
end
runby my_summarize, by(iso3) verbose
exit
Any help would be really appreciated.
Thank you in advance.
Best,
Konstantina
Related Posts with Issue combining foreach and runby
Multilevel logistic regression with cross-sectional data: Why Prob >= chibar2 became 1.000 after adding a level-2 variable?Hello everyone, I'm running a multilevel logistic regression with SHARE Corona Survey. I would like…
Add summary statistics in a graphHey, I am plotting the graph of an event study. I want to plot in the graph some information like t…
issues with clustering in ivreghdfe - insufficient observationsI needed some advice on clustering while using ivreghdfe. My code is - HTML Code: ivreghdfe yvar (…
What does 'egen newvar=cut(var), group(#)' meanDear Stata users, In the funtion -egen-, we can invoke -egen newvar=cut(var), group(#)- to generate…
Comparison of two independent variables in multiple linear regression OLSHello All, I'm working on a research project which will seek to compare the benefits of an addition…
Subscribe to:
Post Comments (Atom)
0 Response to Issue combining foreach and runby
Post a Comment