Announcement

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

  • Macro list manipulation

    Hello Statalist,

    I'm new to macro manipulation in Stata and I have the following implementation questions with macro list manipulation.

    For illustration purpose, suppose I have a local macro called numbers, whose content is "1 2 3 4 5". In real world, it could be much longer, and could be a macro that saves the results from levelsof command in Stata.

    1. Is there a command in Stata that allows me to apply prefix to the macro contents? Suppose I have another macro called letters, which contains "a b c d e". How do I construct a new macro that contains "a1 b2 c3 d4 e5" (or in the simpler case "a1 a2 a3 a4 a5")?

    2. If I want to convert the macro to a vector (matrix), is there a command that does this, as opposed to looping over the content of numbers?

    Would greatly appreciate any help!
    Jun
    Last edited by Jun Wu; 17 Feb 2015, 18:02.

  • #2
    To get from 1 2 3 4 5 to a1 a2 a3 a4 a5 in a local macro you can do

    Code:
    local numbers: subinstr local numbers " " " a", all
    local numbers a`numbers'
    To get from 1 2 3 4 5 to a1 b2 c3 d4 e5, where a b c d e is in a macro named letters:
    Code:
    local mlength: word count `numbers'
    assert `:word count `letters'' == `mlength'
    local combined
    forvalues j = 1/`mlength' {
         local combined `combined' `:word `j' of `letters''`:word `j' of `numbers''
    }
    display `"`combined'"'
    To store the contents of numbers in a matrix
    Code:
    local commasep: subinstr local numbers " " ", ", all
    matrix A = [`commasep']
    matrix list A
    There may be a better way to get the matrix using Mata, but I don't know it.


    Comment


    • #3
      Neat tricks! The "word" command is really helpful. Also is there documentation on how to use statements followed by a colon?

      Comment


      • #4
        The macro chapter in the [P] manual has all of this and more. See especially the section on macro extended functions.

        Comment


        • #5
          It could be done in Mata:

          Code:
           
          
          . local N 1 2 3 4 5
          
          . local L a b c d e
          
          . mata: 
          ------------------------------------------------- mata (type end to exit) ----
          : N = tokens(st_local("N"))
          
          : N
                 1   2   3   4   5
              +---------------------+
            1 |  1   2   3   4   5  |
              +---------------------+
          
          : L = tokens(st_local("L"))
          
          : L 
                 1   2   3   4   5
              +---------------------+
            1 |  a   b   c   d   e  |
              +---------------------+
          
          : L + N 
                  1    2    3    4    5
              +--------------------------+
            1 |  a1   b2   c3   d4   e5  |
              +--------------------------+
          
          : st_local("LN", invtokens(tokens(st_local("L")) + tokens(st_local("N"))))
          
          : end  
          ------------------------------------------------------------------------------
          
          . di "`LN'"
          a1 b2 c3 d4 e5
          .

          Comment


          • #6
            Many thanks for the Mata solution!

            Comment

            Working...
            X