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