Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Joerg Luedicke <joerg.luedicke@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: For loops, If Statements and Delimiters |
Date | Mon, 11 Feb 2013 14:48:25 -0500 |
FWIW, I cannot replicate this. Running the following in Stata 11.2: *------------------------------------------ clear set obs 100 forval i=1/3 { gen var`i' = `i' } # delimit ; local j = 1; foreach x of varlist var1-var3{; forval i = 1/12{; if(`j'<3){; replace `x' = `x'[`i']-`x'[`j'] in `i'; local j = `j' + 1; }; else{; replace `x' = `x'[`i']-`x'[`j'] in `i'; local j = 3; }; }; }; *------------------------------------------ works as expected: <snip> - forval i = 1/12{ - if(`j'<3){ = if(1<3){ - replace `x' = `x'[`i']-`x'[`j'] in `i' <snip> Joerg On Mon, Feb 11, 2013 at 2:41 PM, Nick Cox <njcoxstata@gmail.com> wrote: > Any suggestion? Mine would include not using ; as a delimiter. More > positively, I would always put a space before each brace. > > My vague recollection is that there are some very obscure bugs that > can bite with semi-colons, although a search did not turn up a precise > post, but spaces before braces might be the territory. > > Nick > > On Mon, Feb 11, 2013 at 7:30 PM, Ryan Stevens <stevrl7@gmail.com> wrote: > >> I have been having an issue with using for loop and if statements >> together with a semi-colon delimiter. The following is some example >> code for the problem I've come across: >> >> # delimit ; >> local j = 1; >> foreach x of varlist var1-var3{; >> forval i = 1/12{; >> if(`j'<3){; >> replace `x' = `x'[`i']-`x'[`j'] in `i'; >> local j = `j' + 1; >> }; >> else{; >> replace `x' = `x'[`i']-`x'[`j'] in `i'; >> local j = 3; >> }; >> }; >> }; >> >> Whenever I try running this I get an error on my if statement, where >> Stata interprets "< 3" as a variable name. Any suggestions for loops >> and if statements using delimiters would be greatly appreciated. > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/