0
votes

I would like to import many files (about 200) using Stata's inline dictionary function (as the data is .txt fixed width). This is working for me with any one file, but I can't seem to do it with a loop over all the files. (The name of my dictionary here is fed_emp_dict_1)

I have:

local dir "/~/"
local data : dir "`dir'" files "*.txt"

foreach f of local data 
{
    
infile using fed_emp_dict_1, using("`f'")
    
save "`f'".dta, replace

}

The goal is to append all the files into one dataset (that's been fixed with the specifications of the dictionary).

For this I was trying:

foreach f of local data {


    append using "`f'"
    save d:data_merge_1.dta, replace
}

Stata isn't giving me any errors, but the files aren't being replaced, nor is the composite merge file being created. Please advise.

1
By "the inline function" I think you mean "the infile command". - Nick Cox
The left brace { must be on the same line as foreach. - Nick Cox

1 Answers

1
votes

The quotation marks around your output filename look wrong. Try

 local fileout : subinstr local f ".txt" ".dta", all 
 save "`fileout'", replace