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

st: RE: macro loop embedded in macro loop (Jun Xu)

From   "Nick Winter" <>
To   <>
Subject   st: RE: macro loop embedded in macro loop (Jun Xu)
Date   Wed, 10 Jul 2002 12:37:08 -0400

> -----Original Message-----
> From: Arthur Mu [] 
> Sent: Wednesday, July 10, 2002 12:06 PM
> To:
> Subject: st: macro loop embedded in macro loop (Jun Xu)
> [snip]
> The problem here is: first I 
> tokenize compare 
> before the first while loop, and within the while loop I have another 
> tokenize and the second while loop, which will overwrite the 
> info stored by 
> the first tokenize of `compare'.  To restore the order of the 
> tokenized 
> `compare', I tokenize compare again and use local i=`i'+2 and 
> macro shift 
> `i' to get away with it.  I am not sure if I correctly explained my 
> question, which is "is there an easy way around it?  
> Otherwise if I need to 
> have several tokenize and while loop's, then I will have to 
> restore them 
> each time as I did?"

Tokenize is only one way to work your way through lists; you are
probably best off not using tokenize for both lists.  The macro extended
functions -word count- and -word x of- let you step through a list of
words (such as a varlist) without tokenizing it:

	tokenize `compare', parse(";")
	while `"`1'"'!="" {

		<do things with `compare'>

		local n_var : word count `varlist'
		forval i=1/`nvar' {
			local cur_var : word `i' of `varlist'

			<do things with `cur_var'>

		macro shift


Nick Winter
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index