Statalist


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

Re: st: Parallel lists processing with nested value lists


From   Phil Schumm <[email protected]>
To   [email protected]
Subject   Re: st: Parallel lists processing with nested value lists
Date   Tue, 12 Aug 2008 08:46:00 -0500

On Aug 12, 2008, at 3:53 AM, Gawrich Stefan wrote:
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
<snip>

Not solved
- still many lines

I agree with Nick, especially on the point that if you want to work with strings in Stata, you need to get used to compound double quotes (TIP: using a text editor that will handle these is invaluable). FYI, I would do what you've done in your example 1 (shown above) this way:


sysuse auto.dta, clear
loc ldef var titletext fcolor lcolor bins
loc plist `" `"price "Price of the car in $" green black 5"'"'
loc plist `"`plist' `"headroom "Max. headroom in inches" red black 6"'"'
loc plist `"`plist' `"mpg "Mileage per gallon" blue black 10"'"'

foreach item of loc ldef {
loc `item': list posof "`item'" in ldef
}

foreach plot of loc plist {
tokenize `"`plot'"'
hist ``var'', title("``titletext''") fcolor(``fcolor'') lcolor (``lcolor'') ///
bin(``bins'')
}


Note that the way you need to build up the macro plist at the top is admittedly a bit inefficient, but, quite frankly, I don't know of another way to do it in Stata. What would be nice is if you could extend string literals over multiple lines, either by using the line continuation characters (i.e., ///), or, by permitting, say, compound double quotes to extend across lines (e.g., in Python, line continuation (triggered by \) may be used within strings, and strings delimited by triple quotes (""") may span multiple lines).


-- Phil

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