首页 文章

Stata - 有效附加200个文件(我的方法需要数小时)

提问于
浏览
1

我试图追加约 . 使用Stata的200个文件 . 下面我提供了我用来追加的代码 . 问题是需要花费太长时间 - 超过5个小时 . 最终附加文件有超过2800万个观测值,大小约为2GB . 我认为问题可能是它每次都在节省,因此需要很长时间 . 我也尝试使用临时文件模式 - 但这也需要很长时间 . 另一方面,我的同事在几分钟内使用SAS做了相同的追加 . 我也在下面提供了他的代码 . 如果有人能告诉我如何在Stata中有效地做到这一点,我将非常感激 - 这样就不需要花费数小时 . 非常感谢!

我的Stata代码:

file close _all
    file open myfile using "$OP\filelist_test.txt", read    
    file read myfile line

    cd "$OP"    
    insheet using "`line'", comma clear
    tostring optionconditioncode, replace

    save "$data\options_all", replace

    file read myfile line

    while r(eof)==0{
        insheet using "`line'", comma clear
        tostring optionconditioncode, replace
        append using "$data\options_all"
        save "$data\options_all", replace

        file read myfile line
        }

    file close myfile

我的同事的SAS代码:

data all_text (drop=fname);
      length myfilename $100;
      set dirlist;
      filepath = "&dirname\"||fname;
      infile dummy filevar = filepath length=reclen end=done missover dlm=',' firstobs=2 dsd;
      do while(not done);
        myfilename = filepath;
        input var1
                    var2
                    var3
                    var4
          output;
      end;

1 回答

  • 2

    似乎OP最近没有出现过 . 罗伯特皮卡德在OP提供的Stata论坛_1266381中给出的解决方案如下:

    > Take a look at -filelist- from SSC. It can create a Stata dataset of
    > files (with full path). The help file has an example that does what
    > you want efficiently. Here's a copy:
    >
    > use "csv_datasets.dta", clear
    > local obs = _N
    > forvalues i=1/`obs' {
    >   use "csv_datasets.dta" in `i', clear
    >   local f = dirname + "/" + filename
    >   insheet using "`f'", clear
    >   tempfile save`i'
    >   save "`save`i''"
    > }
    >
    > use "`save1'", clear
    >   forvalues i=2/`obs' {
    >   append using "`save`i''"
    > }
    

相关问题