admin 管理员组

文章数量: 1184232


2024年3月26日发(作者:怎么结束程序运行快捷键)

STATA十八讲:7流程语句

7 流程语句

7.1循环语句:while

任务7.1:用循环语句编写程序,依次列出1,2,3,4,5

程序示例:

*============================begin=================================

capture drop count5

program count5

local i=1 //先将宏名为i的宏值设为1

while `i’<=5 { //判断如果宏值`i’不大于5,就执行{}中的命令,否则跳开

display `i’ //要干的活是显示宏值`i’

local i=`i’+1 //重新设定宏名为i的宏值,令其等于`i’+1

}

end

*============================end==================================

count5

1

2

3

4

5

看看这些结果,`i’最初设为1,因为1小于5,因此显示1;显示完毕紧随其后

的命令,将i重新设定(即加上1),重新设定后`i’现在等于2,由于2仍然小于

5,于是显示2,并再次重新设定i,…,直到`i’与6等价,不再小于等于5,跳出循

环{},执行end.

中国人民大学 陈传波

****************

STATA十八讲:7流程语句

补充知识:数列的表示方法

2 just one number

1 2 3 three numbers

3 2 1 three numbers in reversed order

.5 1 1.5 three different numbers

1 3 -2.17 5.12 four numbers in jumbled order

1/3 three numbers: 1, 2, 3

3/1 the same three numbers in reverse order

5/8 four numbers: 5, 6, 7, 8

-8/-5 four numbers: -8, -7, -6, -5

-5/-8 four numbers: -5, -6, -7, -8

-1/2 four numbers: -1, 0, 1, 2

1 2 to 4 four numbers: 1, 2, 3, 4

4 3 to 1 four numbers: 4, 3, 2, 1

10 15 to 30 five numbers: 10, 15, 20, 25, 30

1 2:4 same as 1 2 to 4

4 3:1 same as 4 3 to 1

10 15:30 same as 10 15 to 30

1(1)3 three numbers: 1, 2, 3

1(2)9 five numbers: 1, 3, 5, 7, 9

1(2)10 the same five numbers: 1, 3, 5, 7, 9

9(-2)1 five numbers: 9, 7, 5, 3, and 1

-1(.5)2.5 the numbers: -1, -.5, 0, .5, 1, 1.5, 2, 2.5

1[1]3 same as 1(1)3

1[2]9 same as 1(2)9

1[2]10 same as 1(2)10

9[-2]1 same as 9(-2)1

-1[.5]2.5 same as -1(.5)2.5

1 2 3/5 8(2)12 eight numbers: 1, 2, 3, 4, 5, 8, 10, 12

1,2,3/5,8(2)12 the same eight numbers

1 2 3/5 8 10 to 12 the same eight numbers

1,2,3/5,8,10 to 12 the same eight numbers

1 2 3/5 8 10:12 the same eight numbers

7.3循环语句:forvalues

程序示例:

*============================begin=================================

forvalues i=1/5 {

display `i’ //和上一个命令完全等价,只是写法更简洁

中国人民大学 陈传波

****************

STATA十八讲:7流程语句

}

*============================end==================================

程序示例3:

*============================begin=================================

forvalues i=4 (-0.2) 0{ //起始值可大于终值,但步长应为负,步长可为小数

display `i’

}

*============================end==================================

用forvalues做循环时,其命令格式为

forvalues lname = range {

commands referring to `lname'

}

其中 range 为

#1(#d)#2 从#1 开始,到#2 结束,步长为 #d

#1/#2 从#1 开始,到#2 结束,步长为 1

#1 #t to #2 从#1 开始,到#2 结束,步长为(#t - #1)

#1 #t : #2 从#1 开始,到#2 结束,步长为(#t - #1)

上述#可以为任何数:负数,小数。

任务7.2:用循环语句编写程序,计算1+2+3+…+100

程序示例:

*============================begin=================================

scalar s=0 //scalar命令生一个标量s

forvalue i=1/100 {

scalar s=s+`i’ //该标量s不断被替换,每一次都被加一次

}

scalar list s

*============================end===================================

7.3循环语句:foreach

任务5.3(按变量循环):打开数据文件wage1,对nonwhite ,female ,married,

numdep,smsa ,northcen ,south ,west,construc,ndurman ,trcommpu,trade ,

services,profserv ,profocc ,clerocc ,servocc 这些变量分别求频数分布。(提

中国人民大学 陈传波

****************

STATA十八讲:7流程语句

示频数分布命令为tab varlist)

*============================begin=================================

use

/ec-p/data/wooldridge/

wage1, clear

tab nonwhite

tab female

tab married

tab numdep

tab smsa

tab northcen

tab south

tab west

tab construc

tab ndurman

tab trcommpu

tab trade

tab services

tab profserv

tab profocc

tab clerocc

tab servocc

*============================end==================================

对凡事讲究效率的现代人来说,这也够烦的,可是while还是forvalues都无能为

力,因为我们所想要的是,对每个变量进行循环。现在是foreach一显身手的时

候了。上面老长的命令,只需要下面几行字。

程序示例:

*============================begin=================================

use wage1,clear

foreach v of varlist nonwhite-servocc {

tab `v’

}

*============================end==================================

用foreach做循环时,其命令格式为

foreach lname {in|of listtype} list {

commands referring to `lname'

}

例如:

foreach lname in any_list {

foreach lname of local lmacname {

foreach lname of global gmacname {

中国人民大学 陈传波

****************

STATA十八讲:7流程语句

foreach lname of varlist varlist {

foreach lname of newlist newvarlist {

foreach lname of numlist numlist {

可以是数值、变量、文件等。

任务7.4:将, , 纵向拼接起来

*============================begin================================

use student, clear

foreach file in economy math {

append using “`file’”

}

*或者

local flist economy math //先将文件名赋于宏flist

foreach file in `flist’ {

append using “`file’”

}

*============================end==================================

任务7.5(按项循环):逐行显示粮食(rice wheat flax maize)和货币(Dollar Lira

Pound RMB)

*============================begin=================================

local grains “rice wheat flax maize”

foreach x of local grains {

di “`x’”

}

global money “Dollar Lira Pound RMB”

foreach y of global money {

di “`y’”

}

*============================end=================================

任务7.6(生成新变量):生成五个新变量b1,b2,b3,b4,b5,每个变量都是均匀分

布随机数

*============================begin=================================

clear

set obs 10

foreach v of newlist b1-b5 {

gen `v'=uniform()

}

中国人民大学 陈传波

****************

STATA十八讲:7流程语句

*============================end==================================

任务7.7(按数值循环):逐行显示1 2 3 4 8 105

*============================begin================================

foreach num of numlist 1/4 8 105 {

di `num'

}

*============================end==================================

这一用法与forvalues较相似,但不要求数据完全是等步长连续,这在很多

情形下会十分方便,比如在我们合并练习2中的数据文件时。

7.4嵌套循环

任务7.8:生成5个变量,10个观察值,使得第i个变量的第j个观察值等于i+j

*============================begin================================

clear

set obs 10

forvalues i=1/5 { //i的初始赋值为1,依次执行2,3,4,5然后退出

gen v`i'=. //将生成一个新变量v1,完成v1的赋值后将生成v2,…

forvalues j=2(2)8 { // j的初值为2,然后为4,6,8,然后退出内层循环

replace v`i'=`i'+`j' in `j' //在第2行替换v1=1+2=3,…

}

}

*============================end==================================

v1 v2 v3 v4 v5

----- -------------------

1. . . . . .

2. 3 4 5 6 7

3. . . . . .

4. 5 6 7 8 9

5. . . . . .

6. 7 8 9 10 11

中国人民大学 陈传波

****************

STATA十八讲:7流程语句

7. . . . . .

8. 9 10 11 12 13

9. . . . . .

10. . . . . .

对照输出结果理解循环是如何执行的。

7.5条件语句

7.5.1简单的条件语句

如果是国产车,则价格降100元,如果是进口车,则价格提升200元。

sysuse auto, clear

gen p1=price-100 if forein==0

replace p1=price+100 if foreign==1

gen p2=cond(foreign==0,(price-100),(price+100))

前两行与该行等价,到当条件满足时,执行紧跟条件后面的内容,否则执行第二个内容。

list p* forei

任务7.9:判断奇偶数。

*============================begin================================

capture program drop odd

program odd

args num

if int(`num’ /2)!=(`num’-1)/2 { //如(8-1)/2取整为3,不等,跳至else

display “num is NOT an odd number”

}

else {

display “num IS an odd number”

}

end

*============================begin================================

odd 1

odd 100

中国人民大学 陈传波

****************

STATA十八讲:7流程语句

任务7.10:检验数据文件中是否有某个变量。

*============================begin=================================

capture program drop check

program check

capture confirm variable `1’

if _rc!=0 {

display “`1’ not found”

exit

}

display “the variable `1’ exists.”

end

*============================end==================================

use hb97,clear

check inc

check exp

7.5.2循环语句套条件语句

任务7.11:求6-500之间能同时被2,3,5整除的数

*============================begin=================================

forvalues x=6/200 {

if mod(`x’,2)==0 & mod(`x’,3)==0 & mod(`x’,5)==0 { //mod()为同余函

di “the ALL common multiple of 2,3 ,and 5 is `x’ “

}

}

*============================end==================================

30

60

90

120

150

180

任务7.12:求6-500之间能同时被2,3,5整除的最小的数

*============================begin=================================

中国人民大学 陈传波

****************

STATA十八讲:7流程语句

forvalues x=6/500 {

if mod(`x’,2)==0 & mod(`x’,3)==0 & mod(`x’,5)==0 {

di “the LEAST common multiple of 2,3 ,and 5 is `x’ “

continue, break

}

}

*============================end==================================

30

7.6复习和练习

(1)打开自己在为上机练习二课后习题而编写的程序,将其中可以用循环语句

改写的命令重新写一遍并执行之

(2)用自己编写的命令,利用hb97中的数据,快速计算出每个县的FGT指数。

中国人民大学 陈传波

****************


本文标签: 循环 语句 变量 命令 步长