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: local macro


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: local macro
Date   Wed, 26 Feb 2014 10:00:47 +0000

I'll repeat a very simple tip that I use repeatedly. -macro list-
tells you what macros you have defined. If you try to "initialise" a
macro with (e.g.)

local j = ""

and follow with -macro list-, no such macro will be listed. That
doesn't mean that there is no point to such a statement.

1. If you used the same-named macro earlier in the program, you may
well need to flush the existing contents.

2. Sometimes people like to initialise macros before a loop as a
matter of personal programming style. If the initial value would just
be empty, that's redundant but harmless, comfort programming as it
were.

Nick
[email protected]


On 26 February 2014 09:10, Nick Cox <[email protected]> wrote:
> Mitch is essentially correct. Otherwise put,
>
> 1. If a macro doesn't exist, references to it are not illegal, but
> Stata substitutes an empty string, or -- equivalently -- ignores the
> reference. The first time a reference to `i' is seen, there is no such
> macro, so the statement is interpreted as
>
> rename d1t1 y
>
> 2. Conversely, assigning a macro an empty string is a way to destroy
> it. (A macro is also destroyed, or disappears, when the corresponding
> program terminates.)
>
> I don't think it's quite true to imply that macros are initialised to
> empty strings. Reversing the statement above, macros can't be empty
> strings just as empty strings can't be macros, except in the flash of
> their disappearance.
>
> Nick
> [email protected]
>
> On 26 February 2014 04:18, Mitch Abdon <[email protected]> wrote:
>> hi, rochelle.
>>
>> more appropriately, it is 'initialized' to nothing.
>> in the first instance, `i' is nothing, not even zero. that's is why you get y not y0.
>> in the next iteration, i is defined as i = `i' + 1 which is 1, thus the y1.. and so on.
>
> On Feb 26, 2014, at 11:53 AM, R Zhang <[email protected]> wrote:
>
>>> this might be a rudimentary question,
>>>
>>> I saw the following code on UCLA stata cite
>>>
>>> http://www.ats.ucla.edu/stat/stata/faq/doublewide.htm
>>>
>>> *********************************
>>> input d1t1 d1t2 d1t3 d2t1 d2t2 d2t3 d3t1 d3t2 d3t3
>>> 4 5 6 5 6 5 6 7 8
>>> 3 4 5 4 5 4 3 4 5
>>> end
>>>
>>>
>>>
>>> generate id = _n
>>> foreach v of varlist d1t1 d1t2 d1t3 d2t1 d2t2 d2t3 d3t1 d3t2 d3t3 {
>>>  rename `v' y`i'
>>>  local i = `i' + 1
>>> }
>>> *********************************
>>>
>>> my question is:  is the i initialized to 0 (default), I ask because
>>> the output is
>>>
>>>      y   y1   y2   y3   y4   y5   y6   y7   y8   id
>>>
>>>  1.  4    5    6    5    6    5    6    7    8    1
>>>  2.  3    4    5    4    5    4    3    4    5    2
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index