如何在Stata中有条件地导出多个数据文件

时间:2014-12-24 22:57:28

标签: export stata

我读取了一个文件夹中的多个dta文件,并以csv格式导出它们。我应该导出样本大小超过30的dta文件。

cd D:\myfolder\
/* There are many dta files in myfolder */
fs *.dta
foreach f in `r(files)' {
    use `f', clear
    export delimited using "D:\csvfolder\mycsvfile_`f'.csv", novarnames replace
}

如何阻止导出包含30个或更少观察值的数据集?

2 个答案:

答案 0 :(得分:4)

使用if

clear

set obs 29
gen t = "should not be here"

tempfile file1
save "`file1'"

clear

set obs 31
gen t = "should be here"

tempfile file2
save "`file2'"

clear

*-----

foreach f in file1 file2 {
    use "``f''", clear
    if _N > 30 {
        export excel using "~/Desktop/mycsvfile_`f'.xls"
    }
}

有关Stata中使用的if的不同概念,请参阅http://www.stata.com/support/faqs/data-management/multiple-operations/

答案 1 :(得分:1)

试试这个

if c(N)>30 export delimited using ...

相关问题