Statalist


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

st: Parallel lists processing with nested value lists


From   Gawrich Stefan <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: Parallel lists processing with nested value lists
Date   Tue, 12 Aug 2008 10:53:34 +0200

Dear Statalisters,

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


Best wishes

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