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   Daniel Marcelino <[email protected]>
To   [email protected]
Subject   Re: st: reading all files in a directory
Date   Sat, 16 Apr 2011 18:13:01 -0300

Thanks for your precious help.

Daniel

On Sat, Apr 16, 2011 at 3:26 PM, Nick Cox <[email protected]> wrote:
> -fs- from SSC might also help.
>
> On Sat, Apr 16, 2011 at 7:14 PM, Eric Booth <[email protected]> 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
>> [email protected]
>> 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/
>



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


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