Statalist The Stata Listserver


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

st: Re: Foreach??


From   "Michael Blasnik" <[email protected]>
To   <[email protected]>
Subject   st: Re: Foreach??
Date   Mon, 23 Oct 2006 18:09:20 -0400

You keep redefining i to be 1 within the loop. (local i=1). Initialize i before the loop and it should work fine

Michael Blasnik

----- Original Message ----- From: "Raphael Fraser" <[email protected]>
To: <[email protected]>
Sent: Monday, October 23, 2006 6:00 PM
Subject: st: Foreach??



I have 12 variables I7-Q7. I would like to run the following code for
each variable in the varlist and save the result d1, d2,  ..., d12.
However, it appears that the macro i is not being incremented. Does
any body know why?

My thought is each time foreach loops over a variable the macro i
should be incremented.

foreach var of varlist I7-Q7 {
use edata, clear
keep if `var' == 1
bysort id (dov): gen etime = dov - dov[_n-1]
keep if etime > 14
drop etime
local i = 1
save d`i', replace
local ++i
}
*
*   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