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

From |
"R.E. De Hoyos" <redeho@hotmail.com> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: Re: Looping questions |

Date |
Tue, 9 Aug 2005 13:26:27 +0100 |

Kelly,

How can I write a fundction that calculates the change between successive observations of 10 elements of this variable? Ie. Assume the data is sorted by date, decending. The first observation is (Var1[_n] -Var1[_n+10])/Var[_n+10], the second observation is (Var1[_n+2]-Var1[_n+11])/Var1[_n+11], and so on...Is this an instance where I can use the -rolling- command? If so, how?

local i=10 while `i'<_N { //"_N" is the number of observations replace myvar = (Var[_n]-Var[_n+`i'])/Var[_n+`i']) if _n=(`i'-9) local i=`i'+1 }

(2) Now suppose I want to do this same calculation across say, a hundred variables Var1-Var100. How can I loop across the variables in an efficiant manner?

if they have a common prefix e.g. var1, var2,...var100 foreach v of var* { local i=10 while `i'<_N { //"_N" is the number of observations replace myvar = (`v'[_n]-`v'[_n+`i'])/`v'[_n+`i']) if _n=(`i'-9) local i=`i'+1 } } If they have different names then see: http://www.stata.com/statalist/archive/2005-08/msg00081.html

(3) A more general type of loop: suppose I have 100 variables, and I want to display the mean and std. Deviation of each varible.Once you have all your variable names in a local macro (vaslist) or if they have a common prefix you can simply:

foreach v in local varlist {

sum `v'

}

I hope this helps,

Rafa

________________________

R.E. De Hoyos

Faculty of Economics

University of Cambridge

CB3 9DE, UK

www.econ.cam.ac.uk/phd/red29/

----- Original Message ----- From: "kelly johnson" <econometrics@hotmail.com>

To: <statalist@hsphsun2.harvard.edu>

Sent: Tuesday, August 09, 2005 1:05 PM

Subject: st: Looping questions

Hi,

I can do all of this interactively, but need help writing loops.

(1) Suppose I have a variable, say Var1

How can I write a fundction that calculates the change between successive observations of 10 elements of this variable? Ie. Assume the data is sorted by date, decending. The first observation is (Var1[_n] -Var1[_n+10])/Var[_n+10], the second observation is (Var1[_n+2]-Var1[_n+11])/Var1[_n+11], and so on...Is this an instance where I can use the -rolling- command? If so, how?

(2) Now suppose I want to do this same calculation across say, a hundred variables Var1-Var100. How can I loop across the variables in an efficiant manner?

(3) A more general type of loop: suppose I have 100 variables, and I want to display the mean and std. Deviation of each varible.

Any help will be greatly appreciated. Please be specific regarding the coding!

Thanks,

kj

_________________________________________________________________

Express yourself instantly with MSN Messenger! Download today - it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/

*

* 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/

* * 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/

**References**:**st: Looping questions***From:*"kelly johnson" <econometrics@hotmail.com>

- Prev by Date:
**RE: st: -nl- error message** - Next by Date:
**st: RE: Generating random variable through rnd and rndwei and Weibull fits** - Previous by thread:
**st: Looping questions** - Next by thread:
**st: looping questions** - Index(es):

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