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

半腔热情 提交于 2019-12-22 03:55:34

问题


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).


回答1:


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'



回答2:


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.



来源:https://stackoverflow.com/questions/25299247/break-a-command-into-several-lines-in-do-file-in-stata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!