Bookmark and Share

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]

st: For loops, If Statements and Delimiters


From   Ryan Stevens <[email protected]>
To   [email protected]
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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index