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

st: forvalues within foreach?


From   <[email protected]>
To   [email protected]
Subject   st: forvalues within foreach?
Date   Tue, 11 May 2004 12:55:51 -0600

Hello Stata-listers ---
I have two small (and clumsy) programs wherein the objective is 
to create a variable for each day over a two year time frame 
(01Jan2000 - 31Dec2001) then assign the value 1 if the subject 
was on service, as defined by two variables:  svcdate & 
svcdate_end.  My programs are pasted below; the first one 
(var_gen) generates the variables as expected (note that I 
limited variable generation to just the first 10 days in 2000).  
The second program, however, executes when run but it does not 
return a 1 where it should.  I suspect that the problem may be 
with the forvalues loop in the foreach statement.  Any advice or 
suggestions?  My ultimate objective is to sum the total number 
of days each subject was on service over the two year period.   
Thank you.  Clint Thompson   

Program #1:
program var_gen
version 8
local N 10
forvalues i = 1(1)`N' {
	gen day`i' = 0
	}
end


Program #2:
program var_rep
version 8
syntax varlist(numeric)
local n 10
foreach var of local 0 {
	forvalues i = 14610(1)`n' {
	replace `var' = 1 if (`i' >= svcdate & `i' <= 
svcdate_end), by(enrolid)
	}
}
end

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