Search code examples
statastata-macros

invalid 'Component' error when looping over files in a folder in stata


I want to append multiple daily market indices data which I downloaded separately and all stored as csv in the same folder. As the first step, I cleaned the data and saved as dta file for each csv file. However, an r(198) error named "invalid 'Component' " was thrown out after the first csv file is cleaned and saved.

The csv files are named in the pattern as "BR-IBOVESPA-BVSP-20150101-20201223.csv", where "BR" is the two letters abbreviation for Brazil, "IBOVESPA" is the name of the market index, "BVSP" is its symbol of Yahoo Finance, and "20150101-20201223" stands for the time range.

Here is my Stata code:

clear all
cd "...\Market-indices"


local myfiles: dir "...\Market-indices" files "*3.csv", respectcase

foreach file of local myfiles {
    import delimited using `file', clear varn(1)
    keep date close
    gen filename = "`file'"
    split filename, p("-")
    rename filename1 country
    rename filename2 index
    drop filename filename3 filename4 filename5
    capture confirm numeric variable close
    if c(rc)!=0 {
        destring close, force replace
    }
    save "`file'.dta", replace
    } 

The command window reports the following:

. cd "...\Market-indices"
...\Market-indices

. 
. 
. local myfiles: dir "...\Market-indices" files "*3.csv", respectcase

. 
. foreach file of local myfiles {
  2.         import delimited using `file', clear varn(1)
  3.         keep date close
  4.         gen filename = "`file'"
  5.         split filename, p("-")
  6.         rename filename1 country
  7.         rename filename2 index
  8.         drop filename filename3 filename4 filename5
  9.         capture confirm numeric variable close
 10.         if c(rc)!=0 {
 11.                 destring close, force replace
 12.         }
 13.         save "`file'.dta", replace
 14.         }
(7 vars, 1,488 obs)
variables created as string: 
filename1  filename2  filename3  filename4  filename5
close: contains nonnumeric characters; replaced as long
(11 missing values generated)
file BR-IBOVESPA-BVSP-20150101-20201223.csv.dta saved
invalid 'Component' 
r(198);

end of do-file

r(198);

Btw, if I run the codes within the loop separately for each csv file, that works well.


Solution

  • The minimal fix needed is

    import delimited using "`file'", clear varn(1)
    

    as your filenames may contain spaces. See help filename for basic documentation.