Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: RE: double quotes, macros, and foreach commands... oh my


From   "Michael Blasnik" <michael.blasnik@verizon.net>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: RE: double quotes, macros, and foreach commands... oh my
Date   Fri, 5 Dec 2003 09:42:38 -0500

In regard to the second question, there is a simple way to get a directory
listing into a macro using Stata 8:

local datfiles: dir . files  "*.dat"

will put all .dat files in the current directory into the local macro
datfiles.  Each file name will be enclosed in double quotes.  You can then
use the code shown at the end of Nick's posting (levels, followed by a
loop).  See -help local- and look for the extended macro function -dir- for
a syntax explanation.

Michael Blasnik
michael.blasnik@verizon.net


----- Original Message ----- 
From: "Nick Cox" <n.j.cox@durham.ac.uk>
To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, December 05, 2003 9:00 AM
Subject: st: RE: double quotes, macros, and foreach commands... oh my


> Donnell Butler
>
<snip>

> > Question 2: (If you haven't had enough) Is there a way (preferably
> > within the foreach command) to have STATA insheet an ASCII file (and
> > later save as STATA format) every file in a folder (without
> > specifying each individual file name)?
>
> You are using Windows here. From within Stata you
> can get Windows to put a list of filenames in
> a file by
>
> !dir /b *.dat > filelist.txt
>
> If the filenames contain spaces you will need to
> edit this file, so that each filename is
> delimited by double quotes.
>
> Then in Stata, you can go
>
> insheet using filelist.txt, clear
>
> If all is well, your filenames are in
> a single string variable -v1-. If this
> is not so, you'll need to carry out
> some surgery. In other words, your
> list of filenames is (for the moment)
> your dataset.
>
> Now you can go
>
> levels v1, local(files)
>
> to put the filenames in a list.
>
> Then the main loop is something like
>
> foreach f of local files {
> insheet using `"`f'"', clear tab
> display `"`f'"'
> local F : subinstr local f ".dat" ".dta"
> save `"`F'"', replace
> }
>
> Warning: code not tested.
>
> Nick
> n.j.cox@durham.ac.uk


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