Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: For loops, If Statements and Delimiters


From   Ryan Stevens <stevrl7@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: For loops, If Statements and Delimiters
Date   Mon, 11 Feb 2013 14:30:48 -0500

Hi all,

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.

Thanks for your time,
Ryan Stevens
*
*   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index