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: How to separate multiple word assignments to a macro


From   Thomas Jacobs <[email protected]>
To   [email protected]
Subject   Re: st: How to separate multiple word assignments to a macro
Date   Sun, 18 Apr 2010 10:56:08 -0500

Nick,

A belated (as always) thanks for your reply.  I had originally
attempted to use tokenize as you describe since the number of entries
in macro TreasRatesDisplay varies with the use of the program and I
had planned to parse the macro to count them.  However, I was able to
implement the method suggested by Michael by using the extended macro
function list with the sizeof function:

local TreasRatesCnt : list sizeof TreasRatesDisplay

forvalues j = 1/`TreasRatesCnt' {

	local varname: word `j' of `TreasRatesDisplay'
	file write tex "`varname' & " %5.3f (round(Results[2*`j'-1,1],0.001))

}

While I am recycling the macro varname in order to work through the
contents of macro TreasRatesDisplay, I believe this accomplishes what
you are suggesting with tokenize.  As may be seen I am building an
output table for LaTeX and wish to use more meaningful variable names
than those I work with in the Stata do file.  Let me know if you
disagree.  Thanks again.

Tom

On Sun, Apr 11, 2010 at 12:12 PM, Nick Cox <[email protected]> wrote:
>
> A small footnote to this excellent answer.
>
> An alternative to
>
> local TreasRatesDisplay `""5 Yr Treas Rate" "10 Yr Treas Rate" "20 Yr
> Treas Rate""'
> local a : word 1 of `TreasRatesDisplay'
> local b : word 2 of `TreasRatesDisplay'
> local c : word 3 of `TreasRatesDisplay'
>
> is
>
> local TreasRatesDisplay `""5 Yr Treas Rate" "10 Yr Treas Rate" "20 Yr
> Treas Rate""'
> tokenize `"`TreasRatesDisplay'"'
> args a b c
>
> There is clearly not much in it in this example.
>
> But
>
> 1. The -tokenize- automatically produces local macros 1, 2, 3, etc. so
> you need not use a, b, c etc. The last line could be dropped.
>
> 2. You could combine the first two lines
>
> tokenize `""5 Yr Treas Rate" "10 Yr Treas Rate" "20 Yr Treas Rate""'
>
> 3. With several elements, not 3, the advantage increases.
>
> Nick
> [email protected]
>
> Michael Norman Mitchell
>
>   I wonder if this would do the trick for you...
>
> local TreasRatesDisplay `""5 Yr Treas Rate" "10 Yr Treas Rate" "20 Yr
> Treas Rate""'
>
> You could extract the three pieces like this...
>
> local a : word 1 of `TreasRatesDisplay'
> local b : word 2 of `TreasRatesDisplay'
> local c : word 3 of `TreasRatesDisplay'
>
> And then you can display the three pieces like this
>
> . di "The first word is `a'"
> The first word is 5 Yr Treas Rate
>
> . di "The second word is `b'"
> The second word is 10 Yr Treas Rate
>
> . di "The third word is `c'"
> The third word is 20 Yr Treas Rate
>
>   The key is using the -compound double quotes- which start with `" and
>
> end with "' . These are like super duper quotes that can hold a quoted
> string, and Stata understand that each quoted pair is a different
> -word-.
>
>   You can even extact the number of words using the "word count"
> function...
>
> . local wc : word count `TreasRatesDisplay'
> . di `wc'
> 3
>
>   I hope this is helpful.
>
> Michael N. Mitchell
> See the Stata tidbit of the week at...
> http://www.MichaelNormanMitchell.com
>
> On 2010-04-09 4.45 PM, Thomas Jacobs wrote:
> > I am trying to create a macro of readable variable names for later
> > output in multiple tables.  Thus, the number of variable names will
> > change.  For example assume I have a case with three values such as
> >
> > 5 Yr Treas Rate, 10 Yr Treas Rate, 20 Yr Treas Rate
> >
> > which I wish to assign to the macro TreasRatesDisplay
> >
> > Is there any way to do this and end up with just 3 values in the macro
> > where each value may have embedded spaces?  Thanks.
>
> *
> *   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/



--
Thomas Jacobs

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