Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: AW: Merging database


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: AW: Merging database
Date   Wed, 29 Apr 2009 21:02:08 +0100

Quite so. The rule is that -foreach ... in- does not unpack any item in
the list you feed it. It takes what you type utterly literally. As you
say, that may or may not bite depending precisely on what you want to do
within the loop. 

Conversely, -foreach ... of- will unpack what it can, given a
(compulsory) signal about what it is unpacking. 

Nick 
[email protected] 

Martin Weiss

So what is the bottom line on this -foreach in- issue? -foreach- passes
on
the space-delimited elements of whatever it finds between "in" and "{",
as
they are, w/o expansion of any kind. If inside the loop you get lucky
and
issue a command that does the unabbreviation for you, kudos to you. But
issuing a command like -replace- that requires a single variable name,
not a
-varlist-, let`s it all crash...


*************
	clear 
	set obs 10
	
	gen x1=9
	gen x2=8
	
	gen y1=9 
	gen y2=0
	
	local i 0
	foreach var in x*{
		loc ++i
		di in red "Times thru the loop: " `i'
		d `var'
// does not work as it requires a single variable	
// replace `var'=8
	}
	
	local i 0
	foreach var in x* y*{
		loc ++i
		di in red "Times thru the loop: " `i'
		d `var'
// does not work as it requires a single variable	
// replace `var'=8
	}
	
*************


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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