Announcement

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

  • Expand something like x1-x10 in mata to x1 x2...x10

    Dear all,

    I want to expand a macro in stata comtain something like "x1-x10"or "bs*" in mata to x1 x2...x10 or "bs1 bs2 ..." and print then only some of them. by order.

    I tried st_macroexpand() , it did not work.

    Thanks for any help!

  • #2
    Hi
    I would go for something like:
    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . mata
    ------------------------------------------------- mata (type end to exit)
    : stata("unab x : make-trunk")
    
    : tokens("`x'")
                  1          2          3          4          5          6
        +-------------------------------------------------------------------+
      1 |      make      price        mpg      rep78   headroom      trunk  |
        +-------------------------------------------------------------------+
    using the -unab- command in Stata.
    Kind regards

    nhb

    Comment


    • #3
      mata desc x* prints Mata objects matching that pattern, though I know no way to store the results in a string vector.

      A custom function could be used to enumerate x1..x10, but I don't know a way to search to see if they exist.

      Code:
      function stringseq(pre,nums){
        n = length(nums)
        x = J(1,n,"")
        for(i=1;i<=n;i++) x[i] = pre + strofreal(nums[i])
        x
      }
      
      stringseq("x",2::5)
              1    2    3    4
          +---------------------+
        1 |  x2   x3   x4   x5  |
          +---------------------+
      I'd be interested in finding an approach that both (i) searches for a pattern in the Mata workspace and (ii) enumerates the results like ds does in Stata.

      Comment

      Working...
      X