Stata Code

File loops

* fancy ways to get files from directories

set more off

cd "C:\My Documents\Stata\test data"

// File paths etc

// Pre-creating them eliminates the compound quotes problem in specifying files

global in_dir "C:\My Documents\Stata\test data\"

global out_dir "C:\My Documents\Stata\test output\"

global in_suffix ".csv"

global out_suffix ".dta"

/***** METHOD 1 ******/

// this method requires file names without whitespace

// note: you can always replace c(pwd) with a path

// specific command - specify file type after "*"

local files : dir "`c(pwd)'" files "*.txt";

// general command for file type

local files : dir "`c(pwd)'" files "*$in_suffix"

foreach file in `files' {

di "`file'"

insheet using `file', comma clear


// saving like this means the saved file will be filename.in_suffix.dta

// fix described below

save "`f'$out_suffix", replace

}

/******* METHOD 2 ******/

// this method *should be* fully flexible regarding file names

/* The command "fs *" gets the list of files in the directory (requires fs be installed) and returns it to r()

Limitation: it gets all the files - if you want to limit it to files with a certain suffix, you will have to do

some string operations on each file name

*/

qui fs *

// r(files) holds the list of file names

foreach f in `r(files)' {

// get the filename

local file_name = "`f'"


// get length of the filename

local name_len = length("`file_name'")


// create the output file name

// eliminates the last 4 characters of the filename - "." and the file type

/* note: a more flexible version would have a local for the length of the expected

suffix, but I was too lazy

*/

local out_file = substr("`file_name'",1,(`name_len'-4)) //gets rid of original file suffix

// get the infile

insheet using "`f'", comma clear

// do manipulations here...

/* Changes the directory to the output directory. This is just to make sure if the

do file gets run multiple times we aren't also reading in the output files

*/

cd "$out_dir"

// doing it like this means the saved file will be filename.in_suffix.dta

save "`f'$out_suffix", replace

// doing it like this eliminates the infile suffix

save "`out_file'$out_suffix", replace

// go back to the infile directory to grab the next file

cd "$in_dir"

}