Statalist The Stata Listserver


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

st: RE: looping over parallel lists - is there a alternative to "for"?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: looping over parallel lists - is there a alternative to "for"?
Date   Thu, 16 Mar 2006 11:19:30 -0000

Thanks for the thanks. A variety of tricks are 
described in a sequel to the article you mention. 

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/03   SJ 3(2):185--202                                 (no commands)
        discusses ways of working through lists held in macros

while some of the same ground is covered in an FAQ: 

FAQ     . . . . . . . . . . . . . . . .  Looping over parallel list in Stata 8
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  K. Crow
        12/04   How do I process parallel lists in Stata 8?
                http://www.stata.com/support/faqs/lang/parallel.html

In essence, -word # of- is your friend. 

Another command that is useful is -tokenize-. Here is a trivial example: 

. sysuse auto, clear
(1978 Automobile Data)

. tokenize execrable dire mediocre good spectacular 

. mac li
[...]
_5:             spectacular
_4:             good
_3:             mediocre
_2:             dire
_1:             execrable

. forval i = 1/5 { 
  2. label def report `i' "``i''" , add
  3. }

. label li report 
report:
           1 execrable
           2 dire
           3 mediocre
           4 good
           5 spectacular

Nick 
n.j.cox@durham.ac.uk 

Gawrich Stefan
 
> I do a lot of routine or repetitive data analysis and use 
> loops and lists
> frequently (btw, thx to Nick Cox, his "How to fact lists with 
> fortitude"
> helped me a lot).
> 
> I'm wondering why the "for"-command is officialy out of date 
> in Stata while
> one of it's best features  - the use of parallel lists - can't be done
> otherwise. Or am I missing something? 
> 
> One example: I routinely map ten different diseases with tmap 
> and save the
> maps as a graphic file. 
> 
> for var var1-var10 \ any "Disease1" "Disease2" [...] \ any "Filename1"
> "Filename2" [...] : ///
> tmap choro X, id(county) map("county-Coordinates.dta") 
> title("Y") \ graph
> export "Z.png", replace 
> 
> In this example each graph is determined by something like a 
> 3-tuple of
> variable, title and saving-filename. 
> This is just basic code, in real life the code blows up and 
> becomes hard to
> read, as we all know, "for" has some disadvantages.  
> So if it can be done in a more structured manner, I would 
> like to know...
> 

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