Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: AW: Macro dissapears after forval or foreach


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: Macro dissapears after forval or foreach
Date   Wed, 24 Feb 2010 15:42:27 +0100

<> 

You can put the list of values into a -local- beforehand, though, and this -local- will stick around:


*************
local mynumbers 0 1

foreach x of local mynumbers{	
	di "value: `x'"
}

token `mynumbers'
di `1' " " `2'
*************

If Stata did make it through the loop, the max of these values must be the endpoint, i.e. the value that was last assumed by `x'...


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]] Im Auftrag von [email protected]
Gesendet: Mittwoch, 24. Februar 2010 15:28
An: [email protected]
Betreff: st: Macro dissapears after forval or foreach

Dear Statalisters,

I'm just curious why running macros in forval or foreach 
disappear after the loop has finished.

Example:

foreach x in 0 1 {	
di "value: `x'"
}
di "value: `x'"

Following Stata 11 help for foreach and forvalues, 
both commands set local macros to given values.
According to the manual locals are available 
during the execution of the program or do-file 
unless they are deleted or overwritten.
So it should be there, shouldn't it? 


With dynamic value lists or "continue" there is some 
sense in preserving the last processed value.

***

Yes, I know workarounds... ;-))

Best wishes 

Stefan




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


*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index