Announcement

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

  • Grouping variables with similar ending

    I have a bunch of variables generated that are the means of other variables I have in a dataset. All the generated variables end with an ending "_z". I would like to be able to create a global variable which contains all of those variables so that I don't have to keep typing them. Also, when I am using the esttab function, I want to suppress those generated variables.
    Any help would be appreciated.

  • #2
    First, terminology: you are looking to create a macro, not a global variable. A local macro will likely suffice for most purposes, though you could also use a global macro. Many here would advise against the latter. You might want to see
    Code:
    help macro
    Note that for your use, a macro may not be needed at all; you may be able to just use wildcards wherever variable lists are accepted. For instance

    Code:
    sum *_z
    You might want to check
    Code:
    help varlist
    If your variable list is more complicated than that, you might use a macro (here, a local macro):

    Code:
    local myvars var1 var2 *_z
    and then use that macro myvars in a command:

    Code:
    sum `myvars'
    Last edited by Hemanshu Kumar; 05 Aug 2024, 23:48.

    Comment


    • #3
      Pedantry is precision that the pedant prefers while the readership may not.

      More pedantry, but the principle of using Stata terminology to discuss Stata is surely a good one.

      I
      I would like to be able to create a global variable which contains all of those variables
      Following Hemanshu Kumar and taking one step further.

      I would like to create a local macro which contains all of those variable names

      when I am using the esttab function
      esttab is a command, not a function. Unlike some other software, there is a sharp division in Stata between commands and functions.


      If you want to suppress a group of variables, you need the names of the complementary set, and

      Code:
      ds *_z, not
      shows some technique. See also findname from the Stata Journal, functionally a superset of ds.

      To manipulate lists of variable names, check out e.g.

      Code:
      help macrolists
      for how to get the difference between sets, and so on.

      Comment

      Working...
      X