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" <>
To   <>
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

----- Original Message ----- 
From: "Nick Cox" <>
To: <>
Sent: Friday, December 05, 2003 9:00 AM
Subject: st: RE: double quotes, macros, and foreach commands... oh my

> Donnell Butler

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

*   For searches and help try:

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