Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: No need for loops?


From   "de la Garza, Adrian" <[email protected]>
To   <[email protected]>
Subject   st: No need for loops?
Date   Wed, 3 Dec 2003 23:09:55 -0500

I am trying to learn from Nick Cox's suggestion that no loops are needed in most cases for data management. However, I can't think of a way to do the following without a loop.
 
I have the next database in panel format and I need to take averages within countries when my dummy variable 'program' indicates it. Basically, I need to generate a variable that computes the 12-month average for the observations immediatly prior to a 1 in 'program'. 
 
Take Brazil in 1983m3, when program = 1. I want my generated variable, let's call it ratio_before, to get the value of the 12-month average right before 1983m3, and put this value in the same line where program = 1. I know this average should be 0.00639. 
 
Then, I also want to take averages for the subsequent 12-months (including the month when program = 1), for the 12-month period after that, etc. These are always annual (12-month) averages for the first year when the program is implemented as indicated by the 1 in the dummy, then for the 2nd year, for the 3rd year, etc., and we can call these variables ratio_1y, ratio2y, ratio3y, ratio4y, and ratio5y. All of these computed averages should go in the same line where program = 1.
 
The only thing is: I should stop taking the averages for the subsequent years if a new program is implemented (i.e., if I find another 1 in my 'program' variable within the period for the average computed).
 
I would very much appreciate any insight on this dull data management procedure.
Thanks.
 
date      country     ratio                program
1982m1	 bra	 0.001943	 0	
1982m2	 bra	 0.003863	 0	
1982m3	 bra	 0.003382	 0	
1982m4	 bra	 0.011459	 0	
1982m5	 bra	 0.018834	 0	
1982m6	 bra	 0.00357	 0	
1982m7	 bra	 0.009157	 0	
1982m8	 bra	 0.014126	 0	
1982m9	 bra	 0	 0	
1982m10	 bra	 0.007243	 0	
1982m11	 bra	 0.008305	 0	
1982m12	 bra	 0	 0	
1983m1	 bra	 0	 0	
1983m2	 bra	 0.000609	 0	
1983m3	 bra	 0	 1	
1983m4	 bra	 0	 0	
1983m5	 bra	 0.002367	 0	
1983m6	 bra	 0.019421	 0	
1983m7	 bra	 0.006601	 0	
1983m8	 bra	 0.005784	 0	
1983m9	 bra	 0	 0	
1983m10	 bra	 0	 0	
1983m11	 bra	 0	 0	
1983m12	 bra	 0.009794	 0	
1984m1	 bra	 0	 0	
1984m2	 bra	 0.008771	 0	
1984m3	 bra	 0	 0	
1984m4	 bra	 0	 0	
1984m5	 bra	 0.064114	 0	
1984m6	 bra	 0.045464	 0	
1984m7	 bra	 0.004001	 0	
1984m8	 bra	 0.006085	 0	
1984m9	 bra	 0.023197	 0	
1984m10	 bra	 0.018621	 0	
1984m11	 bra	 0.011851	 0	
1984m12	 bra	 0.043256	 0	
1985m1	 bra	 0.004194	 0	
1985m2	 bra	 0.030682	 0	
1985m3	 bra	 0.013068	 0	
1985m4	 bra	 0	 0	
1985m5	 bra	 0.021164	 0	
1985m6	 bra	 0.029493	 0	
1985m7	 bra	 0.000325	 0	
1985m8	 bra	 0.007956	 0	
1985m9	 bra	 0.005136	 0	
1985m10	 bra	 0.006204	 0	
1985m11	 bra	 0.001846	 0	
1985m12	 bra	 0.004623	 0	
1986m1	 bra	 0.002585	 0	
1986m2	 bra	 0.001329	 0	
1986m3	 bra	 0	 0	
1986m4	 bra	 0	 0	
1986m5	 bra	 0	 0	
1986m6	 bra	 0	 0	
1986m7	 bra	 0	 0	
1986m8	 bra	 0	 0	
1986m9	 bra	 0	 0	
1986m10	 bra	 0	 0	
1986m11	 bra	 0	 0	
1986m12	 bra	 0	 0	
1987m1	 bra	 0	 0	
1987m2	 bra	 0	 0	
1987m3	 bra	 0	 0	
1987m4	 bra	 0.000409	 0	
1987m5	 bra	 0	 0	
1987m6	 bra	 0	 0	
1987m7	 bra	 0	 0	
1987m8	 bra	 0	 0	
1987m9	 bra	 0	 0	
1987m10	 bra	 0	 0	
1987m11	 bra	 0	 0	
1987m12	 bra	 0	 0	
1988m1	 bra	 0	 0	
1988m2	 bra	 0	 0	
1988m3	 bra	 0	 0	
1988m4	 bra	 0	 0	
1988m5	 bra	 0	 0	
1988m6	 bra	 0.001445	 0	
1988m7	 bra	 0	 0	
1988m8	 bra	 0	 0	
1988m9	 bra	 0	 1	
1988m10	 bra	 0	 0	
1988m11	 bra	 0	 0	
1988m12	 bra	 0.0115	 0	
1989m1	 bra	 0.026783	 0	
1989m2	 bra	 0	 0	
1989m3	 bra	 0	 0	
1989m4	 bra	 0	 0	
1989m5	 bra	 0.000594	 0	
1989m6	 bra	 0	 0	
1989m7	 bra	 0.003353	 0	
1989m8	 bra	 0	 0	
1989m9	 bra	 0	 0	
1989m10	 bra	 0	 0	
1989m11	 bra	 0	 0	
1989m12	 bra	 0	 0	
1990m1	 bra	 0	 0	
1990m2	 bra	 0	 0	
1990m3	 bra	 0	 0	
1990m4	 bra	 0	 0	
1990m5	 bra	 0.00069	 0	
1990m6	 bra	 0	 0	
1990m7	 bra	 0	 0	
1990m8	 bra	 0	 0	
1990m9	 bra	 0	 0	
1990m10	 bra	 0	 0	
1990m11	 bra	 0.001677	 0	
1990m12	 bra	 0	 0	
1991m1	 bra	 0.001733	 0	
1991m2	 bra	 0	 0	
1991m3	 bra	 0	 0	
1991m4	 bra	 0.000556	 0	
1991m5	 bra	 0.001027	 0	
1991m6	 bra	 0	 0	
1991m7	 bra	 0.00672	 0	
1991m8	 bra	 0.006067	 0	
1991m9	 bra	 0.010918	 0	
1991m10	 bra	 0	 0	
1991m11	 bra	 0.008657	 0	
1991m12	 bra	 0.01534	 0	
1992m1	 bra	 0.011084	 0	
1992m2	 bra	 0.007563	 1	
1992m3	 bra	 0.010085	 0	
1992m4	 bra	 0.024224	 0	
1992m5	 bra	 0.01971	 0	
1992m6	 bra	 0.014293	 0	
1992m7	 bra	 0.004867	 0	
1992m8	 bra	 0.004431	 0	
1992m9	 bra	 0.007104	 0	
1992m10	 bra	 0.005402	 0	
1992m11	 bra	 0.00518	 0	
1992m12	 bra	 0.008627	 0	
1993m1	 bra	 0.00179	 0	
1993m2	 bra	 0.005512	 0	
1993m3	 bra	 0.024779	 0	
1993m4	 bra	 0.014369	 0	
1993m5	 bra	 0.020061	 0	
1993m6	 bra	 0.011883	 0	
1993m7	 bra	 0.014873	 0	
1993m8	 bra	 0.002378	 0	
1993m9	 bra	 0.022878	 0	
1993m10	 bra	 0.018786	 0	
1993m11	 bra	 0.021631	 0	
1993m12	 bra	 0.033346	 0	
1994m1	 bra	 0.022492	 0	
1994m2	 bra	 0.014689	 0	
1994m3	 bra	 0.006625	 0	
1994m4	 bra	 0	 0	
1994m5	 bra	 0.003707	 0	
1994m6	 bra	 0.003702	 0	
1994m7	 bra	 0.004783	 0	
1994m8	 bra	 0.004311	 0	
1994m9	 bra	 0.016077	 0	
1994m10	 bra	 0.009737	 0	
1994m11	 bra	 0.014239	 0	
1994m12	 bra	 0.013324	 0	
1995m1	 bra	 0.009602	 0	
1995m2	 bra	 0	 0	
1995m3	 bra	 0.0037	 0	
1995m4	 bra	 0.008651	 0	
1995m5	 bra	 0.030258	 0	
1995m6	 bra	 0.026069	 0	
1995m7	 bra	 0.009378	 0	
1995m8	 bra	 0.013224	 0	
1995m9	 bra	 0.002075	 0	
1995m10	 bra	 0.019404	 0	
1995m11	 bra	 0.011756	 0	
1995m12	 bra	 0.002617	 0	
1996m1	 bra	 0.009528	 0	
1996m2	 bra	 0.018882	 0	
1996m3	 bra	 0.013677	 0	
1996m4	 bra	 0.018324	 0	
1996m5	 bra	 0.018407	 0	
1996m6	 bra	 0.022637	 0	
1996m7	 bra	 0.008907	 0	
1996m8	 bra	 0.003512	 0	
1996m9	 bra	 0.009545	 0	
1996m10	 bra	 0.035147	 0	
1996m11	 bra	 0.015694	 0	
1996m12	 bra	 0.020915	 0	
1997m1	 bra	 0.025927	 0	
1997m2	 bra	 0.013961	 0	
1997m3	 bra	 0.010244	 0	
1997m4	 bra	 0.025583	 0	
1997m5	 bra	 0.033221	 0	
1997m6	 bra	 0.100757	 0	
1997m7	 bra	 0.04749	 0	
1997m8	 bra	 0.023314	 0	
1997m9	 bra	 0.01595	 0	
1997m10	 bra	 0.026974	 0	
1997m11	 bra	 0.009443	 0	
1997m12	 bra	 0.008786	 0	
1998m1	 bra	 0.008509	 0	
1998m2	 bra	 0.014521	 0	
1998m3	 bra	 0.088546	 0	
1998m4	 bra	 0.006353	 0	
1998m5	 bra	 0.010723	 0	
1998m6	 bra	 0.038046	 0	
1998m7	 bra	 0.032796	 0	
1998m8	 bra	 0.000749	 0	
1998m9	 bra	 0	 0	
1998m10	 bra	 0.003922	 0	
1998m11	 bra	 0.002431	 0	
1998m12	 bra	 0.008973	 1	
1999m1	 bra	 0.000492	 0	
1999m2	 bra	 0	 0	
1999m3	 bra	 0.030268	 0	
1999m4	 bra	 0.07875	 0	
1999m5	 bra	 0.014004	 0	
1999m6	 bra	 0.016539	 0	
1999m7	 bra	 0.020166	 0	
1999m8	 bra	 0.01474	 0	
1999m9	 bra	 0.017401	 0	
1999m10	 bra	 0.039186	 0	
1999m11	 bra	 0.027868	 0	
1999m12	 bra	 0.031492	 0	
2000m1	 bra	 0.050823	 0	
2000m2	 bra	 0.039212	 0	
2000m3	 bra	 0.071603	 0	
2000m4	 bra	 0.012468	 0	
2000m5	 bra	 0.002842	 0	
2000m6	 bra	 0.094395	 0	
2000m7	 bra	 0.039404	 0	
2000m8	 bra	 0.061358	 0	
2000m9	 bra	 0.033285	 0	
2000m10	 bra	 0.016642	 0	
2000m11	 bra	 0.023741	 0	
2000m12	 bra	 0.023498	 0	
2001m1	 bra	 0.058109	 0	
2001m2	 bra	 0.023435	 0	
2001m3	 bra	 0.029286	 0	
2001m4	 bra	 0.038925	 0	
2001m5	 bra	 0.073046	 0	
2001m6	 bra	 0.042219	 0	
2001m7	 bra	 0.084842	 0	
2001m8	 bra	 0.026104	 0	
2001m9	 bra	 0.002545	 1	
2001m10	 bra	 0.026873	 0	
2001m11	 bra	 0.00481	 0	
2001m12	 bra	 0.038117	 0	
2002m1	 bra	 0.050344	 0	
2002m2	 bra	 0.024795	 0	
2002m3	 bra	 0.09419	 0	
2002m4	 bra	 0.038057	 0	
2002m5	 bra	 0.006167	 0	
2002m6	 bra	 0.023367	 0	
2002m7	 bra	 0.001347	 0	
2002m8	 bra	 0	 0	
2002m9	 bra	 0.011039	 1	
2002m10	 bra	 0.015559	 0	
2002m11	 bra	 	0	
2002m12	 bra	 	0	
 
 





*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index