Statalist The Stata Listserver


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

RE: st: Appending several files


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Appending several files
Date   Mon, 16 Oct 2006 13:01:48 +0100

Three quite different comments and an alternative. 

1. If you do it this way, you don't need so many braces. 

local j "0"
foreach f in `r(files)' { 
	if `j' == 0 use `f'
	else append using `f' 
	local ++j
}

2. As you want the numeric interpretation, I find 

local j = 0 

clearer, particularly as you are testing its
numeric value later. Stata doesn't care, but 
a code reader might. 

3. If you are a Windows user and accustomed 
to putting spaces in your filenames, this will
fall over unless the names come quoted. 

local j = 0
foreach f in `r(files)' { 
	if `j' == 0 use `"`f'"'
	else append using `"`f'"' 
	local ++j
}

should be safe either way. 

I feel queasy about going through two loops
simultaneously unless it's unavoidable. 
That's mostly just a style prejudice. 
It is avoidable here, although this 
alternative is less scrutable, so to 
speak: 

tokenize `r(files)' 
use `"`1'"' 
mac shift 
local files `"`*'"'

foreach f of local files {
	append using `"`f'"'  
} 

I should guess that there is not much 
efficiency difference for normal problem
sizes. 

Nick 
[email protected] 

[email protected]
 
> Ulrich Kohler already supplied an interesting solution.  I'd 
> like to show how I usually handle this sort of situation 
> (which does come up often -- with either -append- or 
> -merge-). This might be more of an old-fashioned programmer's method.
> 
> local j "0"
> foreach f in `r(files)' { 
>  if `j' == 0 {
>   use `f'
>  }
>  else {
>   append using `f' 
>  }
> local ++j
> }
> 
> One can be more clever by putting the ++j (or j++) inside the 
> -if- statement, if you prefer.
> I haven't yet tried that technique; it might go 
> if `j++' ==0
> (if I have the syntax right -- or is it `=j++' ... ?) or even
> if ~`j++'

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