Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: sequential application of a model to a number of datasets in manner of foreach var of varlist


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: sequential application of a model to a number of datasets in manner of foreach var of varlist
Date   Wed, 19 Apr 2006 14:30:15 +0100

As a footnote to this, note that -fs- from SSC can 
collect a set of filenames for you. 

What -fs- does
==============

-fs- lists the names of files in compact form.  Note that files do not include
directories. With no arguments, -fs- lists names of files in the current directory
or folder. Otherwise, it follows file specifications.  A file specification is a
filename or (more commonly) a set of filenames indicated by the use of wildcards
* or ?. A directory or folder prefix should be given for files outside the
current directory.

See help on -dir- or -ls- for listing the names of files with more detailed
information. Unlike either of those commands, -fs- may be given more than one file
specification. Also unlike either of those commands, -fs- shows any hidden files.

Most pertinently here, the list of filenames is returned in r(files).

Application here
================

So, in the dopiest case in which your .dta files have
a rag-bag of different names, you can go 

fs *.dta

foreach f in `r(files)' {
	use `f', clear 
	<whatever> 
}

Nick 
[email protected] 

Neil Shephard replied to Ben Dwamena 

> > I wish to empirically compare a linear and nonlinear model using
> > sequentially 12 different datasets. How may I specify a looping
> > procedure for the datasets in the manner of foreach var of varlist
> > x1-x12........instead of writing a do-file for each dataset?
> 
> Assuming datasets are called data1.dta, data2.dta,
> data3.dta....data12.dta, then something like...
> 
> forval x = 1/12{
>   use data`x', clear
>   regress [etc.]
>   nl [etc.]
>   [any other commands]
> }
> 
> ..would allow you to do this.
> 
> If your filenames are non-sequential then you would use a 
> foreach loop...
> 
> foreach x in abc cde efg ghi ijk klm mno{
>   use data`x', clear
>   regress [etc.]
>   nl [etc.]
>   [any other commands]
> }
 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index