Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Double -for- loop on one line crashes Stata (resend)

From   Roger Newson <>
Subject   Re: st: RE: Double -for- loop on one line crashes Stata (resend)
Date   Tue, 15 Oct 2002 19:15:46 +0100

At 17:31 15/10/02 +0100, Nick Cox wrote (in reply to Kaleb Michaud):

I don't know if what appears in my mailer
is exactly what you typed, but my advice
is _never_ to place any text on a line after
a left brace { in a loop. Always start a new

Sometimes Stata can work out what you
mean when you do that, but it is not
guaranteed that it will be able to do so.

This is perhaps tacit rather than explicit
in documentation.
Alternatively, if you have set the command delimiter to a semicolon by using

#delim ;

then you should be able to type your code as

forvalues i =1/3 {;forvalues j=1/3 {;replace badcode = badcode + "h`i'_`j'" if hrec`i'_`j'chk>0; } ;};

because then a semicolon is a substitute for a carriage return (which is the usual command delimiter). However, even then, the program is more easy to read (for most people) as

forvalues i =1/3 {;
forvalues j=1/3 {;
replace badcode = badcode + "h`i'_`j'" if hrec`i'_`j'chk>0;
} ;

Most people in the Stata community, most of the time, do not like multi-command lines (although multi-line commands are fairly common). And a right brace


counts as a command. And a phrase ending in a left brace, such as

forvalues i=1\3 {

also counts as a command.

Ideally, Stata is supposed to insist that commands are delimited by a command delimiter, which is either a carriage return or a semicolon, depending on which delimiter is in force. In practice, it sometimes does not do so. However, it is safer to use one delimiter or the other. And it often makes the program more legible to use both, as in the above example.


Roger Newson
Lecturer in Medical Statistics
Department of Public Health Sciences
King's College London
5th Floor, Capital House
42 Weston Street
London SE1 3QD
United Kingdom

Tel: 020 7848 6648 International +44 20 7848 6648
Fax: 020 7848 6620 International +44 20 7848 6620
or 020 7848 6605 International +44 20 7848 6605

Opinions expressed are those of the author, not the institution.

* For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index