Kelly has three questions. Question 1 and 3 don't need any looping at
all.
----------------------
(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?
sysuse "uslifeexp.dta" , clear
// sort by date , descending
gsort -year
gen x10 = (le - le[_n+10])/le[_n+10]
// or, simpler:
drop x10
gen x10 = le/le[_n+10] - 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?
// now for the variables le - le_bfemale. This requires looping:
drop x10
foreach V of varlist le-le_bfemale {
gen c`V' = `V'/`V'[_n+10]-1
}
----------------------------
(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.
// no looping needed at all:
summarize le-cle_bfemale
----------------------------
Hope this helps,
Svend
__________________________________________
Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000 Aarhus C, Denmark
Phone: +45 8942 6090
Home: +45 8693 7796
Email: sj@soci.au.dk
__________________________________________
*
* 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/