Announcement

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

  • add a prefix for all elements in a macro

    Hi all,

    Is there a simple way to add a prefix for all elements in a macro? say I have a macro called var, which contains "price mpg weight". and I would like to have a new macro "eq1:price eq1:mpg eq1:weight".

    Thanks,

  • #2
    Code:
    local var price mpg weight
    
    local var: subinstr local var " " " eq1:", all
    local var eq1:`var'
    This code only works provided each token in the local macro is separated by exactly one space.

    A more robust approach is with a loop:

    Code:
    local var price mpg weight
    
    local newvar
    foreach v of local var {
        local newvar `newvar' eq1:`v'
    }
    If you need the resulting macro to be named var, like the original, you can conclude with -local var `newvar'-.



    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      local var price mpg weight
      
      local var: subinstr local var " " " eq1:", all
      local var eq1:`var'
      This code only works provided each token in the local macro is separated by exactly one space.

      A more robust approach is with a loop:

      Code:
      local var price mpg weight
      
      local newvar
      foreach v of local var {
      local newvar `newvar' eq1:`v'
      }
      If you need the resulting macro to be named var, like the original, you can conclude with -local var `newvar'-.


      Thank you, Clyde. I know all the commands and functions you wrote, but I just didn't know they could be used in these ways to solve my problem!

      Comment

      Working...
      X