how do I loop through file names in stata

stata loop through file names
stata append all files in a folder
loop over different datasets stata
stata loop over list
stata directory of do-file
append loop stata
loop through numlist stata
stata append multiple files

1) Is it possible to create a vector of strings in stata? 2) If yes, is it then possible to loop through the elements in this vector, performing commands on each element?

To create a single string in stata I know you do this:

    local x = "a string"

But I have about 200 data files I need to loop through, and they are not conveniently named with consecutive suffixes like "_2000" "_2001" "_2002" etc. In fact there is no rhyme or reason to the file names, but I do have a list of them which I could easily cut and paste into a string vector, and then call the elements of this vector one by one, as one might do in MATLAB.

Is there a way to do this in stata?

Looping over files in a folder, Unfortunately, Stata tells me when I try to loop that there are too many files in the loop! My code is below -- anyone know what could be going  An Introduction to Loops in Stata: Forvalues, Foreach of, Foreach in, and Nested Loops A Community Resource Created by: Ashley Weyers, University of Arizona March 2019 Getting Started: Loops 101.

On top of Keith's answer: you can also get the list of files in a directory with

    local myfilelist    : dir . files "*.dta"

or more generally

    local theirfilelist : dir <directory name> files <file mask>

See help extended_fcn.

st: Re: loop over file names, Re: st: looping through data files in do-file I'm having trouble building the string with the path and file names. > Each month's data differs only  The foreach command loops through a list while the forvalues loops through numbers. The first line of the code above is very similar to how you would create a macro. The line begins with the command foreach followed by the name I want to use to represent a group (exactly the same as a macro). The word “in” tells Stata it will perform some action on whatever follows the word “in”.

What many people might want the combination of the two as I did. Here it is:

* Create a local containing the list of files. 
local myfilelist : dir "." files "*.dta"

* Or manually create the list by typing in the filenames.
local myfilelist "file1.dta" "file2.dta" "file3.dta"

* Then loop through them as you need.
foreach filename of local myfilelist {
  use "`filename'"
}

I hope that helps. Note that locals/macros are limited by 67,784 characters--watch out for this when you have a really long list of files or really long filenames.

Re: st: looping through data files in do-file, I'm looking for a way to loop this do-file over all the files in. the folder. Try this (​after changing the folder path to suit your setup): local files : dir  On Fri, Aug 3, 2012 at 3:02 AM, Steve Nakoneshny <scnakone@ucalgary.ca> wrote: >> Rather than use a loop, it would be far more effective to simply use -rename-. See the extensive examples available in -help rename-. >> >> If you happen to not be using Stata 12, -renvars- (SCC) would suit here as well. >> >> Lastly, it's Stata, not STATA.

Re: st: looping over all files in a folder, Fortunately, Stata has functionality built in that allows you to identify all the files in a folder. Not just that, but it also places the name of each file  in a single execution of commands in do-files or ado-files. Alternatively, we can save a dataset and use it, and erase it. Compared to this, tempfile is more efficient. Or, one can consider preserve/restore commands, but preserve command stores only one dataset. Syntax step1: tempfile data1 (declaring data name) Step2: commands referring to `data1

looping over files in stata – Stata Tips and Tricks, 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  Re: st: looping over all files in a folder. From: "Dimitriy V. Masterov" <dvmaster@gmail.com> References: st: looping over all files in a folder. From: "Radu Ban" <raduban@gmail.com> Prev by Date: Re: Re: st: least products regression; Next by Date: Re: st: looping over all files in a folder; Previous by thread: st: looping over all files in a

Stata Code - Jennifer Winter, -- This email has been scanned by the Sybari - Antigen Email System. * * For searches and help try: * http://www.stata.com/help.cgi?search We can do so by just tweaking a bit of the codes in the previous example. Define local macro mcode and month, then rename the 12 vars in the foreach loop. > local mcode 0 > local month jan feb mar apr may jun jul aug sep oct nov dec > foreach m of local month { local mcode = `mcode' + 1 rename mtemp`mcode' mtemp`m' }

Comments
  • This won't work if you have local mylist "a aa" bbb "cc c" dd ee "f f" for instance, because the outer quotes will get stripped: stata.com/statalist/archive/2012-10/msg00285.html
  • Information here on maximum macro length is very out of date, unless your Stata itself is very out of date. Consult help limits in your Stata for what applies to you.