Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Recursively build local macro with "" around each addition


From   Richard Herron <[email protected]>
To   [email protected]
Subject   Re: st: Recursively build local macro with "" around each addition
Date   Mon, 13 Feb 2012 15:26:35 -0500

That does it! Thanks for the solution and explanation!

On Mon, Feb 13, 2012 at 15:16, Nick Cox <[email protected]> wrote:
> The problem is that Stata has a default meaning that "  " are string
> delimiters. They are not part of a string by default.
>
> It follows that if you wish Stata to treat " " as literal characters
> to be included within a string, you need other string delimiters and
> Stata provides `" "' for this purpose.
>
> You know this, but the one quoted example you give that doesn't work
> fails because it doesn't satisfy this rule. That is, although the last
> "' could be a string delimiter its partner `" can't be because it
> occurs _within_ the string.
>
> At least, that's my interpretation; and in any case the code below
> seems to do what you want.
>
> * begin code
> local output
> local arg1 ten
> local arg2 one two
> local state lo hi
>
> foreach a1 of local arg1 {
>   foreach a2 of local arg2 {
>       foreach s1 of local state {
>           foreach s2 of local state {
>               local output `" `output' "`a1' `s1', `a2' `s2'" "'
>           }
>       }
>   }
> }
> display `"`output'"'
> * end code
>
> Nick
>
> On Mon, Feb 13, 2012 at 8:01 PM, Richard Herron
> <[email protected]> wrote:
>> I would like to recursively build a local macro that wraps each
>> addition in double quotes.
>>
>> For example, I loop slowly over "lo" and "hi" states of words in the
>> first local macro, and quickly over "lo" and "hi" states of the second
>> local macro. The following code generates a local macro that properly
>> orders the states, but doesn't wrap each addition in double quotes.
>>
>> * begin code
>> local output
>> local arg1 ten
>> local arg2 one two
>> local state lo hi
>>
>> foreach a1 of local arg1 {
>>    foreach a2 of local arg2 {
>>        foreach s1 of local state {
>>            foreach s2 of local state {
>>                local output "`output'" "`a1' `s1', `a2' `s2' "
>>            }
>>        }
>>    }
>> }
>> display "`output'"
>> * end code
>>
>> The output is in the correct order, but doesn't have double quotes
>> around each addition.
>>
>> . ten lo, one lo ten lo, one hi ten hi, one lo ten hi, one hi ten lo, two
>>> lo ten lo, two hi ten hi, two lo ten hi, two hi
>>
>> Instead, I would like the following.
>>
>> . "ten lo, one lo" "ten lo, one hi" "ten hi, one lo" "ten hi, one hi"
>> "ten lo, two
>>> lo" "ten lo, two hi" "ten hi, two lo" "ten hi, two hi"
>>
>> I will use this string as an argument for the -mtitles()- option in
>> -esttab- in SSC's -estout- package (-ssc install estout-).
>>
>> I tried combinations of `' and "" that seemed logical, but got either
>> -invalid syntax- or only the last addition without double quotes and
>> -lo not found-. (E.g., I thought that -local output "`output'" `""`a1'
>> `s1', `a2' `s2' ""'- would provide double quotes, but this also yields
>> -invalid syntax-).
>>
>
> *
> *   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/

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index