楼主: yanwenshou
6087 1

[数据管理求助] 【独家发布】Stata Loop 语句-forvalue/foreach/while/if [推广有奖]

小草

学科带头人

6%

还不是VIP/贵宾

-

TA的文库  其他...

Data collection

论文写作与期刊投稿

国际贸易+农业政策+政治经济学

威望
0
论坛币
574 个
通用积分
97.7502
学术水平
91 点
热心指数
90 点
信用等级
64 点
经验
3779 点
帖子
958
精华
0
在线时间
2065 小时
注册时间
2009-11-9
最后登录
2024-1-2

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
4.2.1 Looping Over Sequences of Numbers (forvalue)
The basic looping command takes the form

forvalues number = sequence {
    ... body of loop using `number' ...
}
Here forvalues is a keyword, number is the name of a local macro that will be set to each number in the sequence, and sequence is a range of values which can have the form

min/max to indicate a sequence of numbers from min to max in steps of one, for example 1/3 yields 1, 2 and 3, or
first(step)last which yields a sequence from first to last in steps of size step. For example 15(5)50 yields 15,20,25,30,35,40,45 and 50.
(There are two other ways of specifying the second type of sequence, but I find the one listed here the clearest, see help forvalues for the alternatives.)

The opening left brace must be the last thing on the first line (other than comments), and the loop must be closed by a matching right brace on a line all by itself. The loop is executed once for each value in the sequence with your local macro number (or whatever you called it) holding the value.
4.2.2 Looping Over Elements in a List (foreach)
The second looping command is foreach and comes in six flavors, dealing with different types of lists. I will start with the generic list:

foreach item in a-list-of-things {
        ... body of loop using `item' ...
}
Here foreach is a keyword, item is a local macro name of your own choosing, in is another keyword, and what comes after is a list of blank-separated words. Try this example

foreach animal in cats and dogs {
        display "`animal'"
}
This loop will print "cats", "and", and "dogs", as the local macro animal is set to each of the words in the list. Stata doesn't know "and" is not an animal, but even if it did, it wouldn't care because the list is generic.

If you wanted to loop over an irregular sequence of numbers for example you needed to do something with the Coale-Demeny regional model life tables for levels 2, 6 and 12-- you could write

foreach level in 2 6 12 {
        ... do something with `level' ...
}
That's it. This is probably all you need to know about looping.
4.2.4 Looping for a While (While)
In common with many programming languages, Stata also has a while loop, which has the following structure

while condition {
        ... do something ...
}
where condition is an expression. The loop executes as long as the condition is true (nonzero). Usually something happens inside the loop to make the condition false, otherwise the code would run forever.

A typical use of while is in iterative estimation procedures, where you may loop while the difference in successive estimates exceeds a predefined tolerance. Usually an iteration count is used to detect lack of convergence.

The continue [,break] command allows breaking out of any loop, including while, forvalues and foreach. The command stops the current iteration and continues with the next, unless break is specified in which case it exits the loop.
4.2.5 Conditional Execution (if)
Stata also has an if programming command, not to be confused with the ifqualifier that can be used to restrict any command to a subset of the data, as in summarize mpg if foreign. The ifcommand has the following structure

if expression {
        ... commands to be executed if expression is true ...
}
else {
        ... optional block to be executed if expression is false ...
}
Here if and the optional else are keywords, type help exp for an explanation of expressions. The opening brace { must be the last thing on a line (other than comments) and the closing brace } must be on a new line by itself.

If the if or else parts consist of a single command they can go on the same line without braces, as in if expression command. But if expression { command } is not legal. You could use the braces by spreading the code into three lines and this often improves readability of the code.

So here we have a silly loop where we break out after five of the possible ten iterations:

        forvalues iter=1/10 {
                display "`iter'"
                if `iter' >= 5 continue, break
        }
And with that, we break out of looping.

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:forvalue foreach Stata while value

本帖被以下文库推荐

工作是最好的休闲方式!
沙发
ellzy_cn 发表于 2014-10-9 16:39:38 |只看作者 |坛友微信交流群
多谢,尽管现在还没用到。

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-18 02:21