Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use global macros inside conditional statements

    Hi guys, I'm encountered a syntax problem that should be easy to solve but I still can't nail how to write.

    I wanted to create a global called countrycode which should have the country code of a country (of course) and then use that global for conditional statements.

    Take the following example, this is the code I'm running (I want to keep those observations where the variable countrycode is equal to the local ccode, which in this case is RWA):
    Code:
    global ccode "RWA"
    list if countrycode==`ccode'
    This is what the console prints:
    . do "/var/folders/x9/1zrmd2qj5qj6dmw592sbwxq40000gn/T//SD51173.000000"

    . global ccode "RWA"

    . list if countrycode==`ccode'
    invalid syntax
    r(198);

    end of do-file

    r(198);
    The capture of the database is below (keep only Rwanda):
    Click image for larger version

Name:	Screenshot 2023-02-12 at 5.24.46 PM.png
Views:	1
Size:	423.7 KB
ID:	1701359


    Thank you for your help!

    Guido

  • #2
    global ccode "RWA"
    list if countrycode==`ccode'
    has two errors. The first is that although you declared ccode as a global macro, in the next line of code you are trying to access it as if it were a local macro. The `...' construction accesses the contents of local macros only. To access the contents of a global macro you would do it as -$ccode-.

    But even that doesn't get you to working code. That's because if you fix only that problem, your second command would read: -list if countrycode == RWA-. Then Stata would try to find a variable named RWA to compare variable countrycode to. Since you have no such variable, you will get a -RWA not found- error message. To compare the variable countrycode to the literal string RWA you need -list if countrycode == "RWA"-. Now, you may think that since you specified quotes around RWA in your definition of macro ccode, they should be there. But Stata automatically strips leading and trailing quotes from macros when you define them. So they aren't there. So you must supply them in the -list- command. So, finally, the code would be:
    Code:
    global ccode RWA // YOU CAN PUT QUOTES AROUND RWA IF YOU WANT, BUT THEY WON'T DO ANYTHING
    list if countrycode == "$ccode"
    That said, there is no reason to use a global macro here. Local macros area safer programming practice and should always be used in preference to global macros unless it is simply not possible. (I have been using Stata since 1994 on a more or less daily basis and in all that time I have only once encountered the need to use a global macro--it's really very rare.) So better is:
    Code:
    local ccode RWA
    list if countrycode == "`ccode'"

    Comment


    • #3
      Perhaps too obvious to state, but here goes.

      Code:
      list if countrycode == "RWA"
      is fine Stata code.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        has two errors. The first is that although you declared ccode as a global macro, in the next line of code you are trying to access it as if it were a local macro. The `...' construction accesses the contents of local macros only. To access the contents of a global macro you would do it as -$ccode-.

        But even that doesn't get you to working code. That's because if you fix only that problem, your second command would read: -list if countrycode == RWA-. Then Stata would try to find a variable named RWA to compare variable countrycode to. Since you have no such variable, you will get a -RWA not found- error message. To compare the variable countrycode to the literal string RWA you need -list if countrycode == "RWA"-. Now, you may think that since you specified quotes around RWA in your definition of macro ccode, they should be there. But Stata automatically strips leading and trailing quotes from macros when you define them. So they aren't there. So you must supply them in the -list- command. So, finally, the code would be:
        Code:
        global ccode RWA // YOU CAN PUT QUOTES AROUND RWA IF YOU WANT, BUT THEY WON'T DO ANYTHING
        list if countrycode == "$ccode"
        That said, there is no reason to use a global macro here. Local macros area safer programming practice and should always be used in preference to global macros unless it is simply not possible. (I have been using Stata since 1994 on a more or less daily basis and in all that time I have only once encountered the need to use a global macro--it's really very rare.) So better is:
        Code:
        local ccode RWA
        list if countrycode == "`ccode'"
        Thank you Clyde and Nick!

        Clyde that solved my problem, I did not know that globals can't be called in the same way as locals (or that putting quotes after declaring a local or global won't do anything as Stata will strip them out).

        Comment

        Working...
        X