Announcement

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

  • How to change local strings

    Let's say I want to change the content of string local named asdf from q to 2. I tried this

    Code:
    . local asdf "q"
    
    . display("`asdf'")
    q
    
    . local asdf subinstr(`asdf',"q","2")
    
    . display("`asdf'")
    subinstr(q,

    As you can see, the last line does not produce what I expect. I expected 2 will come out. How can I solve this problem?


  • #2
    No need to use the subinstr() function to change the value of the macro in this way: just overwrite it.
    Code:
    local asdf q
    display `"`asdf'"'
    
    local asdf 2
    display `"`asdf'"'
    When you write -local asdf whatever-, whatever, viewed as a string, becomes the new value of local macro asdf. If that string happens to look like Stata code for a function evaluation, that is beside the point. The function evaluation is not performed when the value of local macro asdf is changed. If you want the value of local macro asdf to be changed to the value that would result from evaluating that expression (and not just writing out the expresion itself) then you can either calculate the resulting value and directly overwrite the macro, as shown above, or your can force evaluation as follows:

    Code:
    local asdf q
    display `"`asdf'"'
    
    local asdf = substr("`asdf'", "q", "2", .)
    display `"`asdf'"'
    There is also another approach. For string substitution specifically, there is a macro extended function:

    Code:
    local asdf q
    display `"`asdf'"'
    
    local asdf: subinstr local asdf "q" "2", all
    display `"`asdf'"'

    Comment


    • #3
      Thank you. But I would like to replace the content of local variable by replacing q with 2, rather than newly defining it. Would it be possible? It seems your first solution newly defines local variable. Second solution doesn't run. Third solution also seems to be just newly defining new local variable in the sense that the last two lines run without running the first two lines.

      Comment


      • #4
        Clyde is basically right. To change the contents of a local macro, you just redefine it. There isn't another way to do it. Show me a programming language where something like

        Code:
        asdf = "q"
        
        asdf = "2"
        isn't the way to change the value of a string constant within a program. (The above is fine in Mata, for example.) The only idiosyncratic detail here is that spelling out that a local macro is involved.

        There is a typo in the second example, which should be

        Code:
        local asdf q
        display `"`asdf'"'
        
        local asdf = subinstr("`asdf'", "q", "2", .)
        display `"`asdf'"'
        as study of the third example might also suggest.

        See also https://www.stata.com/statalist/arch.../msg01258.html on terminology.

        Comment

        Working...
        X