Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: For loops, If Statements and Delimiters
From
Ryan Stevens <[email protected]>
To
[email protected]
Subject
Re: st: For loops, If Statements and Delimiters
Date
Mon, 11 Feb 2013 14:54:15 -0500
I'm guessing that using Stata 12 should have no effect? Also, this is
code I've extracted from a larger do file and changed a bit to make it
more general. If you're getting it to work, maybe there's something
going on with the rest of my do file that I'm unaware of. But I was
hoping to see if anyone else had had similar issues with delimiters
and for loops. Thanks both of you for responding so quickly.
Ryan
On Mon, Feb 11, 2013 at 2:48 PM, Joerg Luedicke
<[email protected]> wrote:
> 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 <[email protected]> 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 <[email protected]> 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/
*
* 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/