0
votes

I have multiple .dbf files that I would like to import one at a time, change the name of a variable, and save as a .dta file. The folder that contains the .dbf files contains other files as well that I would like Stata to ignore.

Each of the dbf files is named one of the options listed in the local macro mylist, followed by _ward_CTS.dbf. So for example, B_ward_CTS.dbf is one of the files.

My code is the following:

program drop _all
macro drop _all
set more off
cd "/Users/slums-india/cleaning/maps processing/Ward Point 
Maps/Output"
clear

local files : dir "/Users/slums-india/cleaning/maps processing/WardPoint 
Maps/Output" files "*.dbf"

local mylist B C D E FN FS GS HE HW KE

foreach file of local mylist  {

use 'file'_ward_CTS.dbf 

/*import database*/
import dbase "'file'_ward_CTS.dbf", clear

/*rename CTS number variable*/
rename cts$V4 cts_number

save "/Users/slums-india/cleaning/sra/temp/'file'_ward_CTS.dta", replace

} 

I cannot seem to get this loop to run. The error I get is invalid 'file'.

What am I doing wrong?

1

1 Answers

1
votes

You need to delete the first line in the loop, change ' in file and add quotes:

foreach file of local mylist  {
    /*import database*/
    import dbase "`file'_ward_CTS.dbf", clear

    /*rename CTS number variable*/
    rename cts$V4 cts_number

    save "/Users/slums-india/cleaning/sra/temp/`file'_ward_CTS.dta", replace 
}