全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
2383 2
2014-11-13
现在我有大量txt文件,每个文件是一天的数据,我需要将每个文件依次读取然后处理后append。问题出现在读取部分
文件名是有规律的,为 ABCD-YYYYMMDD.txt格式,后面YYYYMMDD为时间。我写了这样的程序,发现Stata什么都没做= =data editor是空的。

forvalue i=1/10{
gen date=`i'+7305
format date %td
gen year=year(date)
gen month=month(date)
gen day=day(date)
tostring year month day,replace
replace month=substr("0"+month,-2,2)
replace day=substr("0"+day,-2,2)
gen date1=year+month+day
*生成了文本格式的日期YYYYMMDD
infile ABCD-`date1'.txt,clear
save date_`i'.dta
}


1.是不是我不应该用gen应该用scalar?但是用scalar的话tostring要求必须有variable
2.最后infile运行的时候总是告诉我file ABCD-.txt not found 是什么原因呢?是不是因为clear,如果去掉clear,那么又会显示
you must start with an empty dataset




在这里跪谢了!
二维码

扫码加我 拉你入群

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

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

全部回复
2014-11-19 17:34:01
cd "d:\test\"
local filelist: dir . files "*.txt"
local a 1
foreach x of local filelist {
        infile using `x', tab clear
        save file`a'.dta
        local a = `a' + 1

}

use file1.dta, clear
local a=`a'-1
forvalue i=2 /`a' {
   append using file`i'
}
二维码

扫码加我 拉你入群

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

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

2014-11-20 22:44:57
ermutuxia 发表于 2014-11-19 17:34
cd "d:\test\"
local filelist: dir . files "*.txt"
local a 1
太谢谢了!我去试一下!
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群