Olena Stavrunova
> After running the following do-file I get the message "
> invalid syntax" after
> the second "while". Please, let me know what is wrong?
>
> -------------------
> clear
> set memory 512m
> set more off
> use "c:\data.dta"
>
> local k1 = 1
> local k2 = 14
> local k3 = 27
> local k4 = 40
>
> local i=1
> while `i'<=4 {
> local j=1
> while `j' <= `k`i'' {
>
> replace lfsocc`j'=1 if lfsocc`j'==lfsocc`k`i''
> replace lfsocc`j'=0 if lfsocc`j'~=1
> local j=`j'+1
> }
> local l=`k`i''-1
> egen occten`i'=rsum(lfsocc1-lfsocc`l')
> keep occten*
> merge using "c:\data.dta"
> drop _merge
> local i=`i'+1
> }
>
I can't see what's wrong. You need
a trace, possibly.
Your code could be simplified using -forval- and
-foreach-, somewhat along the following
lines:
clear
set memory 512m
set more off
use "c:\data.dta"
foreach k in 1 14 27 40 {
forval j = 1 / `k' {
replace lfsocc`j' = 1 if lfsocc`j' == lfsocc`k'
replace lfsocc`j' = 0 if lfsocc`j' ~= 1
}
local l = `k' - 1
egen occten`k' = rsum(lfsocc1-lfsocc`l')
* n.b. `k' above
keep occten*
merge using "c:\data.dta"
drop _merge
}
Nick
[email protected]
*
* 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/