Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: reading all files in a directory


From   Stas Kolenikov <[email protected]>
To   [email protected]
Subject   Re: st: reading all files in a directory
Date   Sat, 16 Apr 2011 12:56:08 -0500

cd "directory name"
local allfiles : dir . files *.dta
clear
gen str80 which = ""
foreach f in `"`allfiles'"' {
   append using `f'
   replace which = `"`f'"' if which == ""
}

On Fri, Apr 15, 2011 at 7:13 PM, Daniel Marcelino <[email protected]> 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/
>



-- 
Stas Kolenikov, also found at http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.

*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index