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
"Nick Cox" <[email protected]>
To
<[email protected]>
Subject
RE: st: How to separate multiple word assignments to a macro
Date
Sun, 11 Apr 2010 18:12:17 +0100
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/