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: can I use a global macro as a trigger?


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: can I use a global macro as a trigger?
Date   Wed, 29 May 2013 23:33:13 +0100

Your idea is perfectly sound. Pedantic purist programmers prefer not
to use globals unless they must -- such people reach for locals -- but
that is not your question. Try it and see:

global foo = 42

if $foo == 42 {
    di "some kind of in-joke?"
}
else di "not reached"

Any notion that  the -if- command expects variables is perhaps based
on confusion with the -if- qualifier. The -if- command expects some
expression to be evaluated as true or false and variables cause some
awkwardness.

For more, see

http://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier/


Nick
[email protected]


On 29 May 2013 23:01, Melanie Leis <[email protected]> wrote:
> Hello all,
>
> I have a .do file for which I need to run different sections depending
> on whether what I'm doing is a standard or a special scenario.
> Ideally, I would like to use a macro to trigger this, so that at the
> beginning of my .do file I just have to specify my global type as
> standard or special, and then have it automatically know which part of
> the code to run.  The idea of what I want to do is something like:
>
> global type standard
>
> if ${type}=="standard" {
>
> "run code"
>
> }
> else if ${type}=="special"{
>
> "run other code"
>
> }
>
> I understand that the if command does not work like this (it's looking
> for variables), but is there a way that I could use this global
> command at the beginning of my .do file to indicate which part of the
> code to run?
>
> Thanks!
>
> Melanie
> *
> *   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