Stata循环语句的运用——循环数值、变量、文件

数值循环

local j=0
while `j'<5{
scalar a=`j'^2
dis a
local j=`j'+1
}
scalar j=0
while j<5{
scalar a=j^2
dis a
scalar j=j+1
}
forvalues i=0/14{
dis `i'
}
forvalues i=0 (-2) -14{
dis `i'
}

变量循环

clear
foreach i of varlist var_1-var_N{
replace `i'="" if `i'=="n.a."
replace `i'=subinstr(`i', "," , "",.)
destring `i',replace
}

对文件进行循环

use 2001-2006,replace
local files "2007-2012 2013-2018"
foreach `v' of local files{
append using v
}
use balance1,replace
forva i=2/3{
append using balance`i'
}
clear
insheet using gov1.csv
forva i=2/3{
preserve
       clear
       insheet using gov`i'.csv
	   save gov`i',replace
restore
merge 1:1  stkcd accper using gov`i'
keep if _m==3
drop _m
}
clear
insheet using gov1.csv
foreach v in gov2 gov3{
preserve
   clear
   insheet using `v'.csv
   save `v',replace
restore
merge 1:1  stkcd accper using `v'
keep if _m==3
drop _m
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值