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 ofdatasets in manner of foreach var of varlis

From   "Ben Dwamena" <[email protected]>
To   <[email protected]>
Subject   RE: st: sequential application of a model to a number ofdatasets in manner of foreach var of varlis
Date   Wed, 19 Apr 2006 10:58:19 -0400

Thanks Neil and Nick

Ben Adarkwa Dwamena, MD

Assistant Professor of  Radiology
Division of Nuclear Medicine
Department of Radiology
University of Michigan Medical School
1500 E. Medical Center Drive
B1  G505   University  Hospital
Ann Arbor, MI 48109-0028

[email protected]

Staff Physician
D748 Nuclear Medicine Service (115), 
VA Ann Arbor Health Care System
2215 Fuller Road
Ann Arbor, MI 48105
734-761-7886 Phone
734-761-5229 Fax

>>> [email protected] 04/19/06 9:30 AM >>>
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
directories. With no arguments, -fs- lists names of files in the current
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
* or ?. A directory or folder prefix should be given for files outside
current directory.

See help on -dir- or -ls- for listing the names of files with more
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 

[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:

Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues.
*   For searches and help try:

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