Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to run the keep command in a do-file in Stata 12:

keep a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 c1 c2 c3 c4

What I want is to do the following:

keep {a1 a2 a3 a4 a5
     b1 b2 b3 b4 b5
     c1 c2 c3 c4}

I know the {} brackets don't do the trick but I'm looking for the command that does it. Using #delimiter ; does not work either.

I want to do this because subgroups of variables have a relation among themselves (which I intended to signal above by using a, b and c) and I want to have that clear in my code. I permanently add and delete variables. Note that I don't want to use the drop command (in which case the solution is trivial).

like image 961
luchonacho Avatar asked Aug 14 '14 02:08

luchonacho


People also ask

How do you break lines in Stata do file?

You can follow the same procedure in do-files—if your editor or word processor will let you—but you can do better. You can change the end-of-line delimiter to ';' by using #delimit, you can comment out the line break by using /* */ comment delimiters, or you can use the /// line-join indicator.

What is break in Stata?

Break tells Stata to cancel what it is doing and return control to you as soon as possible. If you click on Break in response to the input prompt or while you are typing a line, Stata ignores it, because you are already in control.

How do you write notes in Stata do file?

Blank lines and comments in do files are not executed in Stata. To add a comment, type “*” followed by the comment. 2 Comments are automatically color-coded green in do files. Each line of text that you want to “comment-out” needs to start with an asterisk.

What do three slashes mean in Stata?

Commenting your code Multiline comments can be made with /* which tell Stata to ignore everything until it sees */ . Three forward slashes (///) means that the current command is continued on the next line.


1 Answers

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.

like image 165
Roberto Ferrer Avatar answered Sep 23 '22 04:09

Roberto Ferrer