Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: reading all files in a directory


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: reading all files in a directory
Date   Sat, 16 Apr 2011 19:26:17 +0100

-fs- from SSC might also help.

On Sat, Apr 16, 2011 at 7:14 PM, Eric Booth <ebooth@ppri.tamu.edu> wrote:
> <>
>
> 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"
>> }
>>

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index