Announcement

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

  • String substitution in macros?

    Hi all,

    I have a local containing a series of variable names with suffixes:

    local myvars = "foo1_stub1 foo2_stub2 bar1_stub1 bar2_stub2"

    I would like to trim the suffixes (using something analogous to the subinstr function for string vars), to produce:

    ds $myvars
    "foo1_ foo2_ bar1_ bar2_"

    I have a large number of variable names, but a small set of suffixes.

    Thanks,

    Mike

  • #2
    Couldn't you just do this with a replace command in a text or do-file editor?
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Your question contains your answer. Use subinstr, which you can do within one or more loops given enough structure.

      Code:
       
      local myvars  foo1_stub1 foo2_stub2 bar1_stub1 bar2_stub2
      local myvars: subinstr local myvars "stub1" "", all 
      local myvars: subinstr local myvars "stub2" "", all
      Code:
       
      local myvars  foo1_stub1 foo2_stub2 bar1_stub1 bar2_stub2
      foreach s in 1 2 {
           local myvars: subinstr local myvars "stub`s'" "", all 
      }



      Comment


      • #4
        Richard: I am looping over multiple variable lists, so this is not feasible. I should have mentioned this in the original post, but wanted to try to keep my example as uncluttered as I could.

        Nick: Thank you, that worked nicely. Given the slightly different syntax, is this a distinct function from the standard subinstr(s1,s2,s3,n) used for variable operations?

        Comment


        • #5
          See -help extended_fcn-
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment

          Working...
          X