Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Eric Booth <ebooth@ppri.tamu.edu> |
To | "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: reading all files in a directory |
Date | Sat, 16 Apr 2011 18:14:08 +0000 |
<> Here's some code to line up with your R code: **************** *setwd("/Users/DM/Data") loc mydir "/Users/DM/Data" cd "`mydir'" loc files t1 t2 **AC = c() clear sa "AC.dta", replace emptyok **for ( x in files ) { foreach x of local files { **u = read.csv(x, header=T) **read file "x" **u$dataset = x clear insheet using "`mydir'//`x'_2002.csv", delimiter(";") **AC$state = c("AC") gen state = "AC" **AC = rbind(AC, u) append using "AC.dta" sa "AC.dta", replace } **************** YOu should also take a look at the macro extended function "dir" (-help extended_fcn) if you want to avoid having to define your 'files' macro. - Eric __ Eric A. Booth Public Policy Research Institute Texas A&M University ebooth@ppri.tamu.edu Office: +979.845.6754 On Apr 15, 2011, at 7:13 PM, Daniel Marcelino wrote: > Dear all, > > I've a problem to read thousands of CSV files. I used to do that using > R for what I wrote some loops. For teaching purpose I'm converting > these codes into Stata language, so I wondering if someone could drive > me to that. > > setwd("/Users/DM/Data") [here I set the woking > directoryin Stata I do this cd "/Users/DM/Data"] > files = list.files(pattern = "csv$") [here package scan for > every CSV file] > AC = c() [here I create an > emptydata set to allocate all CSV files] > for ( x in files ) { [here I read each file > found in working directory] > u = read.csv(x, header=T) [here in "u" I put every > read file "x"] > u$dataset = x > AC = rbind(AC, u) [finally, here I append every > read file to the `AC'] > AC$state = c("AC") [and generate a flag to which set > of file it came from]. > } > > > I just start writing a similar to Stata. > > cd "/Users/DM/Data" > foreach x in files { > insheet using "/Users/DM/Downloads/v_2002.csv", delimiter(";") > gen state = "AC" > } > > Daniel > > > > -- > Daniel Marcelino > http://danielmarcelino.zip.net > Skype: dmsilva.br > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/