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   David Kantor <[email protected]>
To   [email protected]
Subject   Re: st: Using arguments in a global macro
Date   Mon, 10 Feb 2014 00:10:23 -0500

At 08:17 PM 2/9/2014, Kate Ivanoval wrote:
Hello,

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.

Kate

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.
HTH
--David

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