Dear Stata users,
I am have two variables (ideally I want to do what follows for 5-10 variables): "rgdp_pc" and "exp_conc_ind" and I want to calculate the average for each of these variables by country between 2000 and 2002 (the data I have is a panel). On top of calculating the averages for each variable, I want to collect all the new generated variables in a global (called final) which I want to use later. However, when I am doing that the global that is created only accounts for the final generated variable which will be in that case "exp_conc_ind_avg". Below is the code I am using:
// Create the global
global xlist rgdp_pc exp_conc_ind
// Create a loop
foreach var of varlist $xlist {
egen `var'_avg=mean(`var') if year>=2000 & year<=2002, by(country)
global final `var'_avg
}
//
Hence my question is the following: Is there a way, within the loop, to include all the generated variables in the global I call final?
Many thanks in advance for your kind help.
Best regards,
Samer
I am have two variables (ideally I want to do what follows for 5-10 variables): "rgdp_pc" and "exp_conc_ind" and I want to calculate the average for each of these variables by country between 2000 and 2002 (the data I have is a panel). On top of calculating the averages for each variable, I want to collect all the new generated variables in a global (called final) which I want to use later. However, when I am doing that the global that is created only accounts for the final generated variable which will be in that case "exp_conc_ind_avg". Below is the code I am using:
// Create the global
global xlist rgdp_pc exp_conc_ind
// Create a loop
foreach var of varlist $xlist {
egen `var'_avg=mean(`var') if year>=2000 & year<=2002, by(country)
global final `var'_avg
}
//
Hence my question is the following: Is there a way, within the loop, to include all the generated variables in the global I call final?
Many thanks in advance for your kind help.
Best regards,
Samer
Comment