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
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Using arguments in a global macro
Date
Mon, 10 Feb 2014 09:35:23 +0000
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
[email protected]
On 10 February 2014 05:10, David Kantor <[email protected]> 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/