0

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.

IAN CHAN
  • 3
  • 1
  • 1
    Nothing we can check here. My only guess is that contrary to your question, your filenames are more variable than you let on. Showing us the name of the second file is necessary. – Nick Cox Dec 27 '20 at 09:47
  • Hi Nick, it is nice to have your reply. The names of the following several files are: "CN-Shenzhen Component-399001.SZ-20150101-20201223", "CN-SSE Composite Index-000001.SS-20150101-20201223", "ES-IBEX 35-IBEX-20150101-20201223." Please tell me what kind of extra information should I provide. – IAN CHAN Dec 28 '20 at 05:51

1 Answers1

1

The minimal fix needed is

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

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

Nick Cox
  • 35,529
  • 6
  • 31
  • 47