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
putting multiple graphs for quantitative vs categorical variables in one graphHi, I have 7 variables, which are actually for one survey question that asks respondents to rank th…
How to export the freqs of multiple variables from stata to excelHello , I have been using tabout to output summaries for 2 X 2 tables quite well. Now a new need ha…
Issues with inaccurate generate of a new variableHi there, I have recently used a very simple comand to generate a new variable. gen MFN_zero = Imp…
invalid syntax when referencing to variable labels in loopsI'm trying to refer to the labels of variables (to include these in graph titles) when looping on a …
resetxt locks on small data setWhen I run: Code: resetxt esg totmktval, id(id) it(date) model(xtbe) I get: Code: ===============…
Subscribe to:
Post Comments (Atom)
0 Response to Issue combining foreach and runby
Post a Comment