2016-03-22 8 views
0

私の質問のタイトルはちょっとあいまいかもしれません。以前は、 "サブディレクトリの完全なリストを取得"し、これらのサブディレクトリのファイルをstataに読み込みたいとします(this postthis postを参照)。 @Roberto Ferrerの素晴らしい提案のおかげで、私はほとんどこれを行うことができます。しかし、私は別の問題に遭遇します。私は非常に多くの別々のファイルを持っているので、ローカルマクロの長さは上限に達しているようです。私は最終的に5にサブディレクトリの数を減らすときSTATAは、コマンドlocal n:word countマクロに最大量の文字列を自動的に割り当てる方法は?

macro substitution results in line that is too long 
    The line resulting from substituting macros would be longer than allowed. The maximum allowed length is 645,216 characters, which is calculated on the basis of set maxvar. 

    You can change that in Stata/SE and Stata/MP. What follows is relevant only if you are using Stata/SE or Stata/MP. 

    The maximum line length is defined as 16 more than the maximum macro length, which is currently 645,200 characters. Each unit increase in set maxvar increases the length maximums by 129. The 
    maximum value of set maxvar is 32,767. Thus, the maximum line length may be set up to 4,227,159 characters if you set maxvar to its largest value. 
r(920); 

後、エラ​​ーメッセージを送る、STATAが正常に動作します。約100のサブディレクトリを持つので、私は20回にわたってアクションを複製すると考えます。まあ、それは扱いやすいですが、私はまだ私ができるかどうかを知りたいですこのプロセスを完全に自動化する、より具体的には、最大許容マクロ長を "使い果たし"、ファイルをインポートして次回のサブディレクトリのグループを追加します。ありがとうございました。続き あなたは、マクロ内のファイルのリスト全体を格納する必要はありません。私のコード

//==================================== 
//=== read and clean projects data === 
//==================================== 
version 14 
set linesize 80 
set more off 

clear 
macro drop _all 
set linesize 200 
cd G:\Data_backup\Soufang_data 


*---------------------------------- 
* Read all files within dictionary 
*---------------------------------- 


* Import the first worksheets 1:"项目首页" 2:"项目概况" 3:"成交详情" 
* worksheet1 
filelist, directory("G:\Data_backup\Soufang_data") pattern(*.xlsx) 
* Add pattern(*.xlsx) provent importing add file type(.doc or .dta) 
gen tag = substr(reverse(dirname),1,6) == "esuoh/" 
keep if tag==1 
gen path = dirname+"\"+filename 
qui valuesof path if tag==1 
local filelist = r(values) 

split dirname, parse("\" "/") 
ren dirname4 citylist 
drop dirname1-dirname3 dirname5 
qui valuesof citylist if tag==1 
local city = r(values) 

local count = 1 
local n:word count `filelist' 

    forval i = 1/`n' { 
     local file : word `i' of `filelist' 
     local cityname: word `i' of `city' 

     ** don't add xlsx after `file', suffix has been added 
     ** write "`file'" rather than `file', I don't know why but it works 
     qui import excel using "`file'",clear 
     cap qui sxpose,clear 
     cap qui drop in 1/1 
     gen city = "`cityname'" 


     if `count'==1 { 
      save house.dta,replace emptyok 
     } 
     else   { 
      qui append using house 
      qui save house.dta,replace emptyok 
      } 

     local ++count 
     } 

答えて

2

です。 filelistは、作業したいファイルのデータベースを作成します。それを保存して、処理したいファイルごとにリロードしてください。また、非常に非効率的な方法でデータセットを追加します。追加されたデータセットが増加すると、再読み込みと保存のコストが非常に高くなり、プロセス全体をクロールまで遅くする可能性があります。ここで

遅延応答のため申し訳ありません
filelist, directory(".") pattern(*.xlsx) 
save "myfiles.dta", replace 

local n = _N 

forval i = 1/`n' { 

    use in `i' using "myfiles.dta", clear 

    local f = dirname + "/" + filename 

    qui import excel using "`f'",clear 

    tempfile res`i' 
    save "`res`i''" 

} 

clear 
forval i = 1/`n' { 

    append using "`res`i''" 

} 

save "final.dta", replace 
+0

あなたのExcelファイルを処理する方法のスケッチです。ありがとう、@ロベルトピカード、あなたのアプローチは完全に動作します。誰かが私のような膨大な数のファイルをインポートしたい場合は、doファイルの予期しない終了を防ぐためにコマンドの前に 'cap noi 'を追加する方がいいでしょう – zlqs1985

関連する問題