1 循环语句:while

保存为count5.do :

  1. capture drop count5
  2. program count5
  3. local i=1
  4. while `i'<=5{
  5. display `i'
  6. local i=`i'+1
  7. }
  8. end

执行结果:
image.png
数列的表示方法

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

2 循环语句:forvalues

forvalues i=1/5 { 
display `i’ //和上一个命令完全等价,只是写法更简洁
}
任务:用循环语句编写程序,计算 1+2+3+…+100 
程序示例:
scalar s=0 //scalar 命令生一个标量 s 
forvalue i=1/100 { 
scalar s=s+`i’ //该标量 s 不断被替换,每一次都被加一次
} 
scalar list s

3 循环语句:foreach

任务 (按变量循环):打开数据文件 wage1,对 nonwhite ,female ,married,numdep,smsa ,northcen ,south ,west,construc,ndurman ,trcommpu,trade ,services,profserv ,profocc ,clerocc ,servocc 这些变量分别求频数分布。(提示频数分布命令为 tab varlist)可是 while 还是 forvalues 都无能为力,因为我们所想要的是,对每个变量进行循环。现在是 foreach 一显身手的时候了。上面老长的命令,只需要下面几行字:
use http://fmwww.bc.edu/ec-p/data/wooldridge/wage1, clear
程序示例:

use wage1,clear 
foreach v of varlist nonwhite-servocc { 
 tab `v’ 
 }

image.png
任务:将 student.dta, economy.dta, math.dta 纵向拼接起来

use student, clear 
foreach file in economy math { 
 append using “`file’” 
 } 
*或者
local flist economy math //先将文件名赋于宏 flist 
foreach file in `flist’ { 
 append using “`file’” 
 }

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

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’” 
 }

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

clear 
set obs 10 
foreach v of newlist b1-b5 { 
gen `v'=uniform() 
}

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

foreach num of numlist 1/4 8 105 { 
di `num' 
}

4 嵌套循环

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

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,…
 } 
 }

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
任务 :判断奇偶数。

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

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

capture program drop check
program check
capture confirm variable `1’ 
if _rc!=0 { 
 display “`1’ not found” 
 exit 
 } 
display “the variable `1’ exists.” 
end

image.png

2 循环语句套条件语句

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

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’ “ 
 } 
}

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

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 
 } 
}