Break a command into several lines in do-file in Stata

There are several ways. One is using ///. An example:

clear all
set more off

*----- example data -----

set obs 1

forvalues i = 1/25 {
    gen var`i' = `i'
}

describe

*----- what you want -----

keep var1 var2 ///
    var3-var7 ///
    var8 var11

describe

#delimit will work if used correctly. An example:

<snip>

*----- what you want -----

#delimit ;

keep var1 var2 
    var3-var7 
    var8 var11 ;

#delimit cr

describe

There is still another way. help delimit (which you already knew about) states:

See [U] 16.1.3 Long lines in do-files for more information.

That manual entry points you directly to the relevant information.

I suspect lack of research/effort in this case. A Google search (with "stata + break lines in do files") would have easily gotten you there. I'm not recommending this be your first strategy when trying to solve problems in Stata. Rather, start with Stata resources: I recommend reading

[U] 3 Resources for learning and using Stata

[U] 4 Stata’s help and search facilities.


This is just a very simple trick to complement the real solutions by Roberto. Since you have so many variables, one thing I found sometimes useful is to use macros to group variables, especially if you can use the grouping in more than one occasion.

loca a a1 a2 a3 a4 a5
loca b b1 b2 b3 b4 b5
loca c c1 c2 c3 c4 c5
keep `a' `b' `c'