Announcement

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

  • My code fails to create a local macro!!

    Hi all! I have been encountering error with local macros. The last time I had this issue and got stuck, I simply used global instead (I understand it's not recommended at all) but it worked. But I have gotten stuck again. I have gone through the forums in here and also the stata documentation and tried several methods, but to no avail. So, any help is much appreciated.

    Three variants I tried:


    Code:
    local hhld_variables `" "hhid" "totmemb" "total_absentee" "season" "devreg" "ecobelt" "dist" "dname" "vdcmun" "vname" "urbrur" "ward" "subward" "hhname" "team" "intrdate" "iday" "imonth" "iyear" "intervwr" "suprvisr" "operator" "religion" "fmlymemb" "tot5plus" "q01" "q02" "q03" "q04" "q05" "q061" "q061n" "q062" "q062n" "q063" "q063n" "q064" "q064n" "q065" "q065n" "q07" "q081" "q081unit" "q081rb" "q081ak" "q081pd" "q082" "q082unit" "q082rb" "q082ak" "q082pd" "q083" "q083unit" "q083rb" "q083" "'
    Code:
    local hhld_variables `"psu hhid totmemb total_absentee season devreg ecobelt dist dname vdcmun vname urbrur ward subward hhname team intrdate iday imonth iyear intervwr suprvisr operator religion fmlymemb tot5plus q01 q02 q03 q04 q05 q061 q061n q062 q062n q063 q063n q064 q064n q065 q065n q07 q081 q081unit q081rb q081ak q081pd q082 q082unit q082rb q082ak q082pd q083 q083unit q083rb q083ak q083pd q111 q122 no_rem_sender"'
    Code:
    local hhld_variables psu hhid totmemb total_absentee season devreg ecobelt dist dname vdcmun vname urbrur ward subward hhname team intrdate iday imonth iyear intervwr suprvisr operator religion fmlymemb tot5plus q01 q02 q03 q04 q05 q061 q061n q062 q062n q063 q063n q064 q064n q065 q065n q07 q081 q081unit q081rb q081ak q081pd q082 q082unit q082rb q082ak q082pd q083 q083unit q083rb q083ak q083pd q111 q122 no_rem_sender
    When I try to look at the contents of the macro after running the code with the following:

    Code:
    display "`hhld_variables'"
    macro list _hhld_variables
    the display code returns empty and macro list returns:

    Code:
    local macro `hhld_variables' not found
    r(111);
    Thank you so much in advance!

  • #2
    Well, in your first version, because the local macro itself contains quotes, the expression "`hhld_variables'" is a problem: you can't nest quotes inside quotes. So for that version, you have to use compound double quotes. So your first -display- command needs to be -display `"`hhld_variables'"'-.

    But beyond apart from correcting that, I find no errors in your code and, with that one correction, everything works just fine on my setup:

    Code:
    . local hhld_variables `" "hhid" "totmemb" "total_absentee" "season" "devreg" "ecobelt" "dist" "dname" "vdcmun" "vname" "urbrur" "ward" "subwa
    > rd" "hhname" "team" "intrdate" "iday" "imonth" "iyear" "intervwr" "suprvisr" "operator" "religion" "fmlymemb" "tot5plus" "q01" "q02" "q03" "
    > q04" "q05" "q061" "q061n" "q062" "q062n" "q063" "q063n" "q064" "q064n" "q065" "q065n" "q07" "q081" "q081unit" "q081rb" "q081ak" "q081pd" "q0
    > 82" "q082unit" "q082rb" "q082ak" "q082pd" "q083" "q083unit" "q083rb" "q083" "'
    
    . display `"`hhld_variables'"'
     "hhid" "totmemb" "total_absentee" "season" "devreg" "ecobelt" "dist" "dname" "vdcmun" "vname" "urbrur" "ward" "subward" "hhname" "team" "intr
    > date" "iday" "imonth" "iyear" "intervwr" "suprvisr" "operator" "religion" "fmlymemb" "tot5plus" "q01" "q02" "q03" "q04" "q05" "q061" "q061n"
    >  "q062" "q062n" "q063" "q063n" "q064" "q064n" "q065" "q065n" "q07" "q081" "q081unit" "q081rb" "q081ak" "q081pd" "q082" "q082unit" "q082rb" "
    > q082ak" "q082pd" "q083" "q083unit" "q083rb" "q083"
    
    . macro list _hhld_variables
    _hhld_variables:
                    "hhid" "totmemb" "total_absentee" "season" "devreg" "ecobelt" "dist" "dname" "vdcmun" "vname" "urbrur" "ward" "subward"
                    "hhname" "team" "intrdate" "iday" "imonth" "iyear" "intervwr" "suprvisr" "operator" "religion" "fmlymemb" "tot5plus" "q01"
                    "q02" "q03" "q04" "q05" "q061" "q061n" "q062" "q062n" "q063" "q063n" "q064" "q064n" "q065" "q065n" "q07" "q081" "q081unit"
                    "q081rb" "q081ak" "q081pd" "q082" "q082unit" "q082rb" "q082ak" "q082pd" "q083" "q083unit" "q083rb" "q083"
    
    .
    . local hhld_variables `"psu hhid totmemb total_absentee season devreg ecobelt dist dname vdcmun vname urbrur ward subward hhname team intrdat
    > e iday imonth iyear intervwr suprvisr operator religion fmlymemb tot5plus q01 q02 q03 q04 q05 q061 q061n q062 q062n q063 q063n q064 q064n q0
    > 65 q065n q07 q081 q081unit q081rb q081ak q081pd q082 q082unit q082rb q082ak q082pd q083 q083unit q083rb q083ak q083pd q111 q122 no_rem_sende
    > r"'
    
    . display "`hhld_variables'"
    psu hhid totmemb total_absentee season devreg ecobelt dist dname vdcmun vname urbrur ward subward hhname team intrdate iday imonth iyear inter
    > vwr suprvisr operator religion fmlymemb tot5plus q01 q02 q03 q04 q05 q061 q061n q062 q062n q063 q063n q064 q064n q065 q065n q07 q081 q081uni
    > t q081rb q081ak q081pd q082 q082unit q082rb q082ak q082pd q083 q083unit q083rb q083ak q083pd q111 q122 no_rem_sender
    
    . macro list _hhld_variables
    _hhld_variables:
                    psu hhid totmemb total_absentee season devreg ecobelt dist dname vdcmun vname urbrur ward subward hhname team intrdate iday
                    imonth iyear intervwr suprvisr operator religion fmlymemb tot5plus q01 q02 q03 q04 q05 q061 q061n q062 q062n q063 q063n q064
                    q064n q065 q065n q07 q081 q081unit q081rb q081ak q081pd q082 q082unit q082rb q082ak q082pd q083 q083unit q083rb q083ak q083pd
                    q111 q122 no_rem_sender
    
    .
    .
    . local hhld_variables psu hhid totmemb total_absentee season devreg ecobelt dist dname vdcmun vname urbrur ward subward hhname team intrdate
    > iday imonth iyear intervwr suprvisr operator religion fmlymemb tot5plus q01 q02 q03 q04 q05 q061 q061n q062 q062n q063 q063n q064 q064n q065
    >  q065n q07 q081 q081unit q081rb q081ak q081pd q082 q082unit q082rb q082ak q082pd q083 q083unit q083rb q083ak q083pd q111 q122 no_rem_sender
    
    . display "`hhld_variables'"
    psu hhid totmemb total_absentee season devreg ecobelt dist dname vdcmun vname urbrur ward subward hhname team intrdate iday imonth iyear inter
    > vwr suprvisr operator religion fmlymemb tot5plus q01 q02 q03 q04 q05 q061 q061n q062 q062n q063 q063n q064 q064n q065 q065n q07 q081 q081uni
    > t q081rb q081ak q081pd q082 q082unit q082rb q082ak q082pd q083 q083unit q083rb q083ak q083pd q111 q122 no_rem_sender
    
    . macro list _hhld_variables
    _hhld_variables:
                    psu hhid totmemb total_absentee season devreg ecobelt dist dname vdcmun vname urbrur ward subward hhname team intrdate iday
                    imonth iyear intervwr suprvisr operator religion fmlymemb tot5plus q01 q02 q03 q04 q05 q061 q061n q062 q062n q063 q063n q064
                    q064n q065 q065n q07 q081 q081unit q081rb q081ak q081pd q082 q082unit q082rb q082ak q082pd q083 q083unit q083rb q083ak q083pd
                    q111 q122 no_rem_sender
    The problem is not with your code but with how you are trying to run it. The clue is that it worked for you with global macros but not locals. You are trying to run this code one line at a time. You can't do that with local macros. When you run a single line of code, or a group of highlighted lines, any local macro that is defined within that line/group of lines disappears once that line/group of lines finishes executing. So whenever you define a local macro in one place and then refer to it somewhere else, you must run all of the code from beginning to end uninterrupted.

    Global macros are not subject to this limitation. By definition, they are global, and they live as long as Stata remains in memory, unless you specifically eliminate them. This convenient behavior is one of the reasons that people often use global macros even though they are not a safe programming practice. By contrast, local macros exist only within localities in the code such as single programs, or blocks of code highlighted and executed. This is sometimes inconvenient, but it is what makes local macros safer to use.

    Comment


    • #3
      See also https://journals.sagepub.com/doi/pdf...36867X20931028 for a longer discussion of Clyde Schechter's main point about local macros.

      Comment


      • #4
        This is amazing. Thank you so much Clyde Schechter !!! Thank you for the additional resource Nick Cox !!!

        Comment

        Working...
        X