Announcement

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

  • Negating namelists

    Hi
    Namelists are positively specified, eg *r means all names ending on r.
    Sometimes it is easier to specify the opposite, here: All names not ending on r.
    Is there some way to do this?

    This question could just as well have been posted in the Stata forum, but problem occured in Mata, so here it is
    Kind regards

    nhb

  • #2
    If you can create two macros, one containing all names, and the other containing all names ending in r, then you can perhaps modify the approach in the following example with Stata variables. (Note that in this specific example, the second describe will fail if there are no variables ending in r.)
    Code:
    // make some variables
    clear
    set obs 1
    gen aa = 1
    gen bb = 2
    gen ar = 3
    // run the example
    quietly describe, varlist
    local vars `r(varlist)'
    quietly describe *r, varlist
    local omit `r(varlist)'
    local want : list vars - omit
    display "`want'"

    Comment


    • #3
      In addition, see ds and findname (SJ).

      Comment


      • #4
        Excellent, just what I needed.
        Thank you both very much.
        Kind regards

        nhb

        Comment


        • #5
          Just found a strict mata solution myself:
          Code:
          . // Get a dataset
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . mata
          ------------------------------------------------- mata (type end to exit) --------------------------
          :         // Get the varnames
          :         x=st_varname((1..st_nvar()))'
          
          :         x
                             1
               +----------------+
             1 |          make  |
             2 |         price  |
             3 |           mpg  |
             4 |         rep78  |
             5 |      headroom  |
             6 |         trunk  |
             7 |        weight  |
             8 |        length  |
             9 |          turn  |
            10 |  displacement  |
            11 |    gear_ratio  |
            12 |       foreign  |
               +----------------+
          
          :         // Select the varnames ending with e
          :         select(x, regexm(x, "e$"))
                     1
              +---------+
            1 |   make  |
            2 |  price  |
              +---------+
          
          :         // Select the varnames NOT ending with e
          :         select(x, !regexm(x, "e$"))
                             1
               +----------------+
             1 |           mpg  |
             2 |         rep78  |
             3 |      headroom  |
             4 |         trunk  |
             5 |        weight  |
             6 |        length  |
             7 |          turn  |
             8 |  displacement  |
             9 |    gear_ratio  |
            10 |       foreign  |
               +----------------+
          
          : end
          Kind regards

          nhb

          Comment


          • #6
            Very nice. Thanks for posting that example. Working with Stata metadata like variable names is not an aspect of Mata I'd thought of before, and will be a welcome addition to my toolkit. I guess I'm going to have to spend some time with [M].

            Comment

            Working...
            X