Announcement

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

  • how to use 'local' function

    Code:
        local psm SOE i.prodPhaseA i.devFormB i.oprPhaseB hi
        local psm: subinstr local psmtuple "i.*" "",all
        dis "`psm'"
    Here is an example. I have a litst of i.* variables, how to use 'local' function to drop all i.* variables?
    Thanks!

  • #2
    Edit: misread your question.

    This can't be done using the macro parsing -subinstr- or similar functions because they don't allow for pattern matching. However, you can do this with a simple regular expression.

    Code:
    . local varlist a b i.c i.d e
    . local newlist = stritrim(ustrregexra("`varlist'", "(i.)\w", "", .))
    . macro list _newlist
    _newlist:       a b e
    Last edited by Leonardo Guizzetti; 21 Dec 2021, 11:51.

    Comment


    • #3
      Note that local is a command, not a function.

      Comment


      • #4
        Originally posted by Leonardo Guizzetti View Post
        Edit: misread your question.

        This can't be done using the macro parsing -subinstr- or similar functions because they don't allow for pattern matching. However, you can do this with a simple regular expression.

        Code:
        . local varlist a b i.c i.d e
        . local newlist = stritrim(ustrregexra("`varlist'", "(i.)\w", "", .))
        . macro list _newlist
        _newlist: a b e
        Great, this is what I want. Thanks!

        Comment


        • #5
          Originally posted by Leonardo Guizzetti View Post
          Edit: misread your question.

          This can't be done using the macro parsing -subinstr- or similar functions because they don't allow for pattern matching. However, you can do this with a simple regular expression.

          Code:
          . local varlist a b i.c i.d e
          . local newlist = stritrim(ustrregexra("`varlist'", "(i.)\w", "", .))
          . macro list _newlist
          _newlist: a b e
          Oh my god, this is not correct. It also modified "high" in the example.

          Code:
          local varlist a b i.c i.d e high
          local newlist = stritrim(ustrregexra("`varlist'", "(i.)\w", "", .))
          macro list _newlist

          Comment


          • #6
            You just need to escape the period character, otherwise it is interpreted as a wildcard rather than a period:

            Code:
            local varlist a b i.c i.d e high
            local newlist = stritrim(ustrregexra("`varlist'", "(i\.)\w", "", .))
            macro list _newlist
            
            _newlist:       a b e high

            Comment


            • #7
              You're right ... the pattern is not corect. Try this instead

              Code:
              . local varlist a b i.c i._d e high i.high i.island island
              . local newlist = stritrim(ustrregexra("`varlist'", "(i\.[_0-9a-z]+)", "", .))
              . macro list _newlist
              _newlist:       a b e high island

              Comment


              • #8
                Originally posted by Ali Atia View Post
                You just need to escape the period character, otherwise it is interpreted as a wildcard rather than a period:

                Code:
                local varlist a b i.c i.d e high
                local newlist = stritrim(ustrregexra("`varlist'", "(i\.)\w", "", .))
                macro list _newlist
                
                _newlist: a b e high
                Thanks, yet also something wrong:
                Code:
                local varlist firmAge  SOE i.prodPhaseA i.devFormB
                local newlist = stritrim(ustrregexra("`varlist'", "(i\.)\w", "", .))
                macro list _newlist

                Comment


                • #9
                  Code:
                  local newlist = stritrim(ustrregexra("`varlist'", "(i\.)[^ ]*", "", .))

                  Comment


                  • #10
                    Originally posted by Ali Atia View Post
                    You just need to escape the period character, otherwise it is interpreted as a wildcard rather than a period:

                    Code:
                    local varlist a b i.c i.d e high
                    local newlist = stritrim(ustrregexra("`varlist'", "(i\.)\w", "", .))
                    macro list _newlist
                    
                    _newlist: a b e high
                    Thanks Ali Atia .

                    The alternative to what I was trying to do:

                    Code:
                    local newlist = stritrim(ustrregexra("`varlist'", "(i\.)\w+", "", .))

                    Comment


                    • #11
                      Thank you both @Leonardo Guizzetti @Ali Atia, I need to try to learn the regular expression.

                      Comment

                      Working...
                      X