Dear Stata users,
I would really appreciate some help on the following issue. Before that, my apologies if it has already been solved somewhere else, and I did not notice it. Suppose that we have a panel data set which contains three variables.
Thanks in advance,
Cruz A. Echevarria
I would really appreciate some help on the following issue. Before that, my apologies if it has already been solved somewhere else, and I did not notice it. Suppose that we have a panel data set which contains three variables.
- A numerical variable: "ccode" representing the numerical code of each country in the data set [1, 2, 3,....].
- A string variable: "cname" representing the name of each country in the data set [Albania, Afghanistan, Algeria,...].
- A numerical variable: "y" representing...
- The purpose is to generate a collection of scalars: "y_mean_Albania", "y_mean_Afghanistan", "y_mean_Algeria",.... containing the sample mean of variable "y" for each country in the data set.
- I have not been able to do it. I have figured out, however, to generate this collection of scalars: "y_mean_1", "y_mean_2", "y_mean_3",.... But his requires to check the list of ccode's and ccname's to see to which country scalar belongs, say "y_mean_i" represents [some of my code is included]
Code:
scalar year_T = 2011; scalar year_F = 1990; scalar year_L = 2016; quiet use MyData.dta, clear; levelsof ccode, local(levels); foreach i of local levels {; quiet drop if year < year_T - 1; quiet drop if year > year_F; quiet drop if ccode != `i'; display `i' " " cname; quiet sum y, detail; scalar y_mean_1_`i' = r(mean); scalar y_mean_2_`cname' = r(mean); quiet use MyData.dta, clear; }; /* This works */ foreach i of local levels {; scalar list y_mean_1_`i'; }; /* This does not work */ scalar list y_mean_2_Albania;
Cruz A. Echevarria
Comment