Statalist The Stata Listserver


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

st: RE: 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: RE: looping over parallel lists - is there a alternative to "for"?
Date   Thu, 16 Mar 2006 11:49:12 -0000

An apparent limit on the -tokenize- 
approach is that it overwrites previously 
defined sets of macros with names 1, 2, 
etc. So it may seem that you cannot work 
with two or more sets of numbered macros.
But of course you can. 

I have suggested previously that -tokenize-
be generalized to allow a stub to be specified 
so that tokenizing a list 

frog toad newt 

with stub -a- would define macros -a1-, -a2-, -a3-. 
This can be done without waiting for StataCorp 
to see the light, and is implemented in -tknz- 
on SSC. 

program def tknz, rclass 
*! NJC 1.1.0 2 June 2000 
	version 6.0 
	gettoken list 0 : 0, parse(",") 
	syntax , Stub(str) [ * ] 
	tokenize `"`list'"' , `options'  
		
	local i = 1 	
	while "``i''" != "" { 
		c_local `stub'`i' `"``i''"'  
		local i = `i' + 1 
	} 
end 	

The version on SSC is not up-to-date: 

* 2.0.0 NJC 12 June 2005
program tknz, rclass 
	version 8   
	gettoken list 0 : 0, parse(",") 
	syntax , Stub(str) [ * ] 
	tokenize `"`list'"' , `options'  
		
	local i = 1 	
	foreach l of local list { 
		c_local `stub'`i' `"``i''"'  
		local ++i  
	} 
end 	


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

Nick Cox
 
> 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/
> 

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