Announcement

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

  • Macro substraction does not work?

    Why does this not work?


    Code:
        local f data.txt
        local not .txt
        disp "`f'"
        local fileout : list f - not
        disp "`fileout'"

    I thought I am doing exactly the same as suggested in #2 here: https://www.statalist.org/forums/for...tring-elements

  • #2
    I guess the other doesnt work because Stata thinks data.txt is one element so there is nothing to substract?

    Actually, for this problem using subinstr is better

    Code:
        local f data.txt
        disp "`f'"
        local fileout = subinstr("`f'",".txt","",.)
        disp "`fileout'"

    Comment


    • #3
      .txt is not a "word" in your local macro. As you've realised what you want to do is a matter of string manipulation not a matter of removing an element from a set.

      Comment


      • #4
        As an aside, although there is nothing wrong with the approach in #2, I'll just point out that the third command can also be done as
        Code:
        local fileout: subinstr local f ".txt" ""
        I don't think there's any particular performance advantage to one of these vs the other, although I think this one is a bit easier on the eye.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          As an aside, although there is nothing wrong with the approach in #2, I'll just point out that the third command can also be done as
          Code:
          local fileout: subinstr local f ".txt" ""
          I don't think there's any particular performance advantage to one of these vs the other, although I think this one is a bit easier on the eye.
          In (much) older versions of Stata, the limit of 244 characters in a string would affect the function subinstr() but not the (extended) macro function : subinstr.

          Comment

          Working...
          X