Statalist The Stata Listserver


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

st: RE: Question for Statalist about "Creating a macro for files"


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Question for Statalist about "Creating a macro for files"
Date   Wed, 19 Apr 2006 17:38:25 +0100

-outsheet- will only support output using one file, 
so your -outsheet- command is illegal. 

I count three .dta files here, and three .xls files. 

I guess that you want the equivalent of 

use oilgs, clear 
outsheet using bra1.xls
use data2, clear 
outsheet using bra2.xls
use data3, clear 
outsheet using bra3.xls

Using -foreach- is not going to help you much here. 
There is too much irregular detail that is not 
reducible. 

You could do this. 

local i = 1 
foreach file in oilgs data2 data3 { 
	use `file', clear 
	outsheet using bra`i'.xls 
	local ++i 
} 

but that replaces 6 lines with 6. 
I could reduce the code to 5 lines, but 
at the cost of some simplicity. 

As said, the loop is not much simpler than 
the code without a loop.

If you do have an example with 5 files, 
the loop is going to help you a bit more. 

Nick 
n.j.cox@durham.ac.uk 
 
> I am trying to use the foreach command to "outsheet" five statafiles
> into 5 excel files but don't know how to create the two lists. 
> 
> So far I have this...
> 
> #delimit; 
> foreach file in oilgs.dta  data2.dta data3.dta {;
> outsheet using bra1.xls bra2.xls bra3.xls , replace;
> };
>  
> 
> But it doesn't recognize the list of  xls files 

 

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