[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
austin nichols <[email protected]> |

To |
[email protected] |

Subject |
st: re: averaging within a variable |

Date |
Tue, 12 Jul 2005 23:24:55 -0400 |

Assuming you don't mind the alteration of your original problem (Kit objected to the fact that my code creates the even-numbered obs. of Var2 as the desired means, with the odd-numbered values equal to missing), looping over variables is easy. foreach v of varlist var1-var100 { g m_`v'=(`v'+`v'[_n-1])/2 if mod(_n,2)==0 } The above assumes the variable are -order-ed in the intuitive way. If all the variables you want to construct the means for start with "var" you could also: foreach v of varlist var* { g m_`v'=(`v'+`v'[_n-1])/2 if mod(_n,2)==0 } If you _really_ want the means coded in the first fifty observations, and the last fifty observations to be missing, and you don't have Stata 9, or you happen not to want to use Kit's mata code, you could: foreach v of varlist var1-var100 { g m3=(`v'[_n*2-1]+`v'[(_n+1)*2-2])/2 if _n<=50 } -----Original Message----- From: kelly johnson [mailto:[email protected]] Sent: Tuesday, July 12, 2005 10:49 PM To: [email protected] Subject: RE: st: re: averaging within a variable now suppose I had to do this operation in 100 different variabes (call them var1-var100), how can i code a simple loot to do that? i want to perform this within each variable: gen Var2=(Var1+Var1[_n-1])/2 if mod(_n,2)==0 >From: Kit Baum <[email protected]> >Reply-To: [email protected] >To: [email protected] >Subject: st: re: averaging within a variable >Date: Tue, 12 Jul 2005 22:32:13 -0400 > >Austin said >g Var2=(Var1+Var1[_n-1])/2 if mod(_n,2)==0 > >-----Original Message----- >From: kelly johnson [mailto:[email protected]] >Sent: Tuesday, July 12, 2005 9:51 PM >How can I create a new variable whose first observation is the average of >Var1[1] and Var1[2], second observation is the avaerage of Var1[3] and >Var1[4], etc. There will be a total of 50 observations for this new >variable. Thanks in advance! >-----Original Message----- > >Not quite. That creates the even-numbered obs. of Var2 as the desired >values, with the odd-numbered values equal to missing. To fix that up, >thanks to Bill Gould's excellent tutorial at this morning's Boston SUG >meeting, you may use a cute mata trick (in Stata 9, anyway): * * 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/

**Follow-Ups**:**Re: st: re: averaging within a variable***From:*austin nichols <[email protected]>

- Prev by Date:
**Re: st: re: averaging within a variable** - Next by Date:
**Re: st: J. Scott__spost9_ado** - Previous by thread:
**RE: st: re: averaging within a variable** - Next by thread:
**Re: st: re: averaging within a variable** - Index(es):

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