Statalist


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

st: RE: Parallel lists processing with nested value lists


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Parallel lists processing with nested value lists
Date   Tue, 12 Aug 2008 13:32:38 +0100

I am not clear precisely what your argument is. 

The essence of this is to put what you want in macros like 

local l1  "price Price~of~the~car~in~$ green black 5"

and then take them out again as a series of constituent words. It's the
putting them in that's going to be a pain, and very likely that will
take quite as much programmer time as any other saving being discussed. 

On a detail: you use ~ as a filler for spaces to avoid compound double
quotes. What's your objection to compound double quotes? They're fiddly,
but in total less fiddly than this ad hoc filler technique, which is
error prone (miss one space and you have garbage) and lacking in
generality (~ is a character many people use). I guess you'll not
dispute either comment. 

I don't think it's an absolute that brevity of code is always to be
sought. Long-winded code is sometimes clearer, and even sometimes much
efficient. 

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

Gawrich Stefan


Phil Schumm
(http://www.stata.com/statalist/archive/2008-08/msg00258.html) 
pointed out that having values for each run of a loop together in one
local
or nested in a list 
is less error-prone than having them in several locals (like in the
parallel
lists syntax). 
Beside a few exceptions I agree with him.  

I tried to set up this approach in Stata (in a do-file) in two versions.

1) One local for each run  
2) Values for all runs nested in one local 

The following examples produces three histograms with different vars,
titles, colors and bin numbers: 
 
***************  - 1 -   ********************************
sysuse auto.dta, clear
**************   INPUT   
// 1. local "ldef" defines local names that show up in the histogram
syntax
later 
// 2. locals l1, l2 .. lx contain values for run 1,2 .. x of the loop 
// (I use ~ for blanks inside the locals and replace them later to avoid
compound quotes)
local ldef "var titletext fcolor lcolor bins"   
local l1  "price Price~of~the~car~in~$ green black 5" 
local l2 "headroom Max.~headroom~in~inches red black 6" 
local l3 "mpg Mileage~per~gallon blue black 10"

************** Data Preperation (nothing to change here)  ************ 
// How many locals? : Local lmax
local i = 1
while "`l`i''"  !="" {
local lmax = `i'
local i = `i' + 1
}
local wordn : word count `ldef' // number of words in locals 
forval x = 1/`lmax' { //  Loop over locals l1 to l`lmax'
forval y = 1/`wordn' { // Loop over words of local  l`x' 
local def : word `y' of `ldef' 
local val : word `y' of `l`x'' 
local val = subinstr("`val'","~", " " , .)  // exchange each ~ with a
blank
local `def' = "`val'" // Local names from `ldef' get values from local
l`x'

}

**************   USER COMMAND(S) (using the specified local names)
********
histogram `var' , title("`titletext'") fcolor(`fcolor') lcolor(`lcolor')
bin(`bins')
} // End of loop




***************  -  2 -  ********************************
sysuse auto.dta, clear
**************   INPUT   
// A local with nested lists. The first item list contains the local
names,
// list 2 .. x contain values for run 1 .. (x-1) 
// (I use ~ for blanks inside the locals and replace them later to avoid
compound quotes)
local input `"  "var titletext fcolor lcolor bins" "price
Price~of~the~car~in~$ green black 5" "headroom Max.~headroom~in~inches
red
black 6" "mpg Mileage~per~gallon blue black 10" "'

************** Data Preperation (nothing to change here)  ************ 
local lmax : word count `input' //  How many wordgroups in local input
local word1 : word 1 of `input' // extract first wordgroup (definition
group) 
local wordn : word count `word1' // count words in first wordgroup
forval x = 2/`lmax' { //  Loop over wordgroups of local starting from 2
(first run)
local word : word `x' of `input' 
forval y = 1/`wordn' { // Loop over items of wordgroup x of local input
local def : word `y' of `word1' 
local val : word `y' of `word' 
local val = subinstr("`val'","~", " " , .)  // exchange each ~ with a
blank
local `def' = "`val'" // Local names from first wordgroup get values
from
wordgroup x   
}
**************   USER COMMAND(S) (using the specified local names)
********
histogram `var' , title("`titletext'") fcolor(`fcolor') lcolor(`lcolor')
bin(`bins')
} // End of loop

************************************************************************
****
********


Advantages compared to "classic" parallel lists processing:
- The values used in one run are in one place and not spread over
several
locals 
- Local names are defined at the beginning. No manual double creation of
locals. 
- The do-file adjusts not only to the number of runs but also to the
number
of values.

Not solved
- still many lines


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