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

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


From   "Arthur Mu" <[email protected]>
To   [email protected]
Subject   st: macro loop embedded in macro loop (Jun Xu)
Date   Wed, 10 Jul 2002 11:06:14 -0500

All Stata-Users:

I am writing an ado program that one macro loop embedded in another one. I have figured a way around it, but kind of awkward, not sure if there is an easy way to do it.

The command will be like:

mycommand var1 var2 var3 var4, model(reg) compare(1 2; 4 2; 3 2)

I just extracted part of the Program that I am having problems with:

*******************************************************
capture program drop mycommand
program define mycommand
syntax [varlist] ,model(string) compare(string)
tokenize `compare', parse(" ; ")
local i=1

while "`1'"~=""{
...Do whatever I want for the macro's in `compare'
...
...
tokenize `varlist'
while "`1'"~=""{

...Do whatever I want for the macro's in `varlist'
}

tokenize `compare', parse (" ; ")
local i=`i'+2
macro shift `i'
}
end
****************************************************

What I am trying to do with `varlist' within the while loop is to capture individual variable information and store it into a matrix. While the `compare' macro list is to run through each pair within "compare (1 2; 4 2; 3 2)", in which "1 2" encodes two equations by a group variable like female vs. male or white vs. black. 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?"

Jun Xu
Doctoral Student
Department of Sociology
Indiana University


_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx

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