Dear Stata members
I have the following sample data
Now I want to get the average for each variable ( "count_aus", "count_ind" and "count_us") for each year
So my basic code is
Now there is no year in my data but I think I can get it from the month though I tried to use "year" function it didnt work but if at all I can create year some how, how can I create loop to get the average values of all varialbes in one code.
Something that starts like this
foreach v of var count_* {
Can someone help me here. I am not very comfortable with loops so I tried but to no avail. As I have some 43 variables if I try using egen, it will be tedious.
I have the following sample data
Code:
clear input str10 month int total float(count_aus count_ind count_us) "01-01-1998" 11968 .03 .09 .01 "01-02-1998" 11328 .01 .1 .02 "01-03-1998" 12571 .01 .05 .02 "01-04-1998" 11951 .02 .03 .02 "01-05-1998" 12753 .02 .06 .04 "01-06-1998" 11933 .02 .04 .08 "01-07-1998" 11776 .03 .05 .02 "01-08-1998" 11800 .03 .1 .02 "01-09-1998" 12657 .02 .02 .05 "01-10-1998" 13029 .05 .01 .15 "01-11-1998" 12264 .04 .02 .05 "01-12-1998" 12140 .02 .09 .04 "01-01-1999" 12545 .02 .06 .14 "01-02-1999" 11366 .01 .02 .08 "01-03-1999" 12671 .04 .05 .31 "01-04-1999" 12302 .01 .05 .54 "01-05-1999" 12602 .01 .02 .4 "01-06-1999" 12151 .01 .02 .17 "01-07-1999" 11617 .05 0 .07 "01-08-1999" 11952 .02 .03 .04 "01-09-1999" 12037 .03 .29 0 "01-10-1999" 13051 .02 .07 .02 "01-11-1999" 12063 .02 .04 .05 "01-12-1999" 12127 .02 .05 .11 "01-01-2000" 12456 .02 .04 .02 "01-02-2000" 11758 .02 .04 .14 "01-03-2000" 13022 .01 .04 .05 "01-04-2000" 12022 .02 .03 .03 "01-05-2000" 12386 .02 .03 .03 "01-06-2000" 12140 .05 .08 .02 "01-07-2000" 12398 .01 .05 .06 "01-08-2000" 12753 .05 .04 .04 "01-09-2000" 13219 .02 .01 .05 "01-10-2000" 14055 .03 .01 .01 "01-11-2000" 13361 .01 .01 .04 "01-12-2000" 12844 .05 .02 .06 end
Now I want to get the average for each variable ( "count_aus", "count_ind" and "count_us") for each year
So my basic code is
Code:
egen mean_count_aus=mean(count_aus), by ( year )
Something that starts like this
foreach v of var count_* {
Can someone help me here. I am not very comfortable with loops so I tried but to no avail. As I have some 43 variables if I try using egen, it will be tedious.

Comment