Economics should be open

October 1, 2008

stata, double quotes, file names with spaces, foreach

Filed under: Data Insights, Stata — Tags: , , , , , , , — howardchong @ 9:59 pm

It took me more than 10 minutes to figure out, so I’m posting this tip here.

I needed to loop over several files. Pseudocode would look like this:

global files a.csv b.csv c.csv

foreach file of global files {



Problem was that my filenames had spaces. I tried the following, but it didn’t quite work.

global files “a data.csv” “b data.csv” “c data.csv”


The solution that finally worked was to use Stata’s annoying double quotes:


global filelist `””STOXX 600 1of3.csv” “STOXX 600 2of3.csv” “STOXX 600 3of3.csv””‘


Note the extra `” and “‘ at the ends.



  1. Given the insane quotes in Stata,
    can you see why this displays a correct file and path but gives an error if I drop the same expression in use”…”?

    Thanks for your note way back in 2008.

    Comment by Global Health Partnerships, USA — October 21, 2010 @ 6:14 pm

  2. code snippet: for prior comment
    version 9
    global files `””CPT_02_2009.dta” CPT_03_2009.dta” CPT_04_2009.dta””‘
    global path “i:\My_Docs_on_I\BEHRdata\CPT_2009\”
    foreach x of global files {
    display “$path””`x'”

    Comment by Global Health Partnerships, USA — October 21, 2010 @ 6:16 pm

    • Make sure you have double quotes around each item in global files. You are missing the left quote on the 2nd and 3rd elements.

      Comment by howardchong — October 21, 2010 @ 6:27 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: