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

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


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: double quotes, macros, and foreach commands... oh my
Date   Fri, 5 Dec 2003 14:00:01 -0000

Donnell Butler

> Question 1. what can I do to have STATA insheet a series of non-.RAW
> files using the foreach command?
>
> Consider the following example:
> foreach file in "F:\Projects\C-6430
> \Dissertation\Montclair\100000268936" "F:\Projects\C-6430
> \Dissertation\Montclair\100000566461"
>   insheet using "`file'", clear tab
>   display "`file'"
>   save "`file'", replace
> }
>
> Stata will read in the files and display the names if the files
> are .raw. Then, STATA will save the files as .dta
>
> Yet, the actual files are not .raw, they are .dat. And,
> there are far
> more than those presented in this example which would make renaming
> each extension for each file (by the way they are also in different
> folders) quite painful.
>
> I have tried a number of combinations of quotes attempting
> to get at:
>
> insheet using "`file'.dat", clear tab
> display "`file'.dat"
> save "`file'.dta", replace
>
> But clearly that isn't (nor any other variations that I've
> tried) the
> correct quotes method to use. Moreover, if I were to just in the
> macro file using "F...\100000268936.dat", but then I can't save the
> file as dta using the macro command "`file'" without saving the
> file .dat
>
> In a perfect world, I could tell STATA to replace .raw as
> the default
> to insheet with .dat and that would solve everything. So, if anyone
> can tell me how to do that... or any other potential solution for
> this dilemma, I would be very thankful.

Your first problem I don't really understand. As you realise, Stata
[N.B. http://www.stata.com/support/statalist/faq/#spell]
needs to know that the extension is .dat. You can specify
this on each filename, as (what is clearly better) you
can specify this once within a loop. It is not clear why
this isn't working here, and you don't say what results
or error messages you get. Irrespective of that, things
often work better if you use / as a separator: Stata
understands that in talking to Windows it needs to
translate to \.

> 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