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: Using arguments in a global macro


From   "Kate Ivanova" <kivstata@gmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Using arguments in a global macro
Date   Mon, 10 Feb 2014 15:56:13 -0500

Thank you, David and Nick. I now have a much better idea of the use of
macros. I really appreciate your help.

Kate

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: Monday, February 10, 2014 7:22 AM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Using arguments in a global macro

Perhaps I should add that standard Stata style might frown on the use of
globals here at all.

Your code is clear enough here as the global is defined a few lines away
from the program, but in principle it could be defined anywhere in your
code. That's a nightmare to debug.

I'd go so far as to say that I would wonder about any use of globals in a
program other than those defined by Stata itself.

Nick
njcoxstata@gmail.com


On 10 February 2014 09:35, Nick Cox <njcoxstata@gmail.com> wrote:
> You could use delayed evaluation as David suggests, but I find to hard 
> to think of any real advantages to that.
>
> Note also that your program defines -var3- regardless of input. Its 
> input is mapped to local macro `var' but you don't ever use that 
> local.
>
> The following is closer to what I take to be standard Stata style.
>
> global x "var1 var2'"
> program prog1
>                args var
>                gen `var' = 1
>                reg depvar $x `var', noconstant end
> prog1 var3
>
> The whole idea here is that you can wait until you know the name of 
> something before you use it. Otherwise put, you can't act until you 
> know that name, so being patient until you do is fine.
>
> The discussion in Gabi Huiber's blog at 
> http://enoriver.net/index.php/2011/02/10/delayed-macro-substitution/
> may be of interest.
>
> Nick
> njcoxstata@gmail.com
>
>
> On 10 February 2014 05:10, David Kantor <kantor.d@att.net> wrote:
>
>> x was assigned its value before prog1 was defined, and certainly 
>> before
>> prog1 was invoked. At the time that x was assigned its value, var was
empty.
>> So you got what you asked for -- not what you meant to ask for.
>>
>> If you move the assignment to x so it is inside prog1, after -args 
>> var-, then it should work as expected.
>>
>> But presumably, you really want to have the assignment of x right 
>> where you put it, but arranged so that it evaluates var at a later 
>> point. You may be able to use delayed-evaluation. This works for 
>> locals; I've never tried it with globals, but I don't see why it 
>> wouldn't work. Leave the assignment of x where you have it. But put a "\"
before `var' -- thus:
>>
>>
>> global x "var1 var2 \`var'"
>>
>> That will probably work.
>
> At 08:17 PM 2/9/2014, Kate Ivanoval wrote:
>
>>> I am interested whether it is possible to include a command line 
>>> argument in a global macro. So far, I have tried to do that but it 
>>> does not seem to work. What I have looks approximately like this:
>>>
>>> global x "var1 var2 `var'"
>>> program prog1
>>>                args var
>>>                gen var3=1
>>>                reg depvar $x, noconstant end
>>> prog1 var3
>>>
>>> My "var3" variable is actually not a constant but I just used the 
>>> simplest example to illustrate my point. When I run this program, I 
>>> get the regression results but "var3" is simply omitted.
>>>
>>> I would appreciate any help with this. If I need to clarify my 
>>> question, please let me know.
*
*   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/

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