stata小小白

005 Stata循环:while

2019-07-13  本文已影响2人  凡有言说

Stata中的循环有三类:

先介绍while循环

while exp { 
     stata_commands 
}

Braces must be specified with while, and

  • the open brace must appear on the same line as while;
  • nothing may follow the open brace, except, of course, comments; the
    first command to be executed must appear on a new line;
  • the close brace must appear on a line by itself.

while循环的逻辑是:

在具体应用时,while循环可以嵌套在其他循环中。

*单循环
. local j = 1

. 
. while `j' < 10{
  2.         disp `j'
  3.         local j = `j'+1
  4. }
1
2
3
4
5
6
7
8
9
*嵌套循环
. local i = 1

. while `i' <= 5{
  2.         local j = 1
  3.         while `j' < `i'{
  4.                 disp "`j' 小于 `i'"
  5.                 local j = `j' + 1
  6.         }
  7.         local i = `i' + 1
  8. }
1 小于 2
1 小于 3
2 小于 3
1 小于 4
2 小于 4
3 小于 4
1 小于 5
2 小于 5
3 小于 5
4 小于 5

参考资料:
【爬虫俱乐部】精通Stata之数据整理

上一篇下一篇

猜你喜欢

热点阅读