Announcement

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

  • Global macro: How to remove a variable?

    I created a macro of available variable names in my dataset ($vitamins):

    Code:
    global vitamins vita vitb vitc vitd vite
    I created another macro of variables names which I would like to exclude ($vitamins_to_exclude):

    Code:
    global vitamins_to_exclude vitd vite
    I would like to generate a third macro of variable names which I would like to include in my analyses ($vitamins_to_include), but both of these command don't work:

    Code:
    global vitamins_to_include $vitamins - $vitamins_to_exclude
    Code:
    global vitamins_to_include : list $vitamins - $vitamins_to_exclude
    What is wrong with these commands? How can I code this correctly?

  • #2
    Just to clarify, the end result that I am seeking is $vitamins_to_include to contain:

    Code:
    vita vitb vitc

    Comment


    • #3
      Type global(macname) if you wish to refer to a global macro.
      Code:
      global vitamins_to_include : list global(vitamins) - global(vitamins_to_exclude)

      Comment


      • #4
        Bjarte has provided excellent advice.

        I offer an example using locals or globals.

        Code:
        local abcde a b c d e
        local cde c d e
        local ab : list abcde - cde
        di "`ab'"
        
        global abcde a b c d e
        global cde c d e
        global ab : list global(abcde) - global(cde)
        di "${ab}"
        There are two related Stata tips that may also help provide some broader context.
        1) Stata tip 91: Putting unabbreviated lists into local lists
        2) Speaking Stata: Problems with lists

        Comment

        Working...
        X