Announcement

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

  • String concatenation

    Stata15/Linux64
    I was trying to find a solution to this problem in the forums, but I'm still running into this issue when using various strings:

    Code:
    local a1 "v[\`i']<\`l' (v[\`i']-1)<\`l' v[\`i']<10"
    works

    Code:
    local a1 "v[\`i']<\`l'" + "(v[\`i']-1)<\`l'" + "v[\`i']<10"
    works, but di "`a1'" fails.

    Code:
    local a1 "v[\`i']<\`l' ///
    (v[\`i']-1)<\`l' v[\`i']<10"
    doesn't work

    Code:
    local a1 ""v[\`i']<\`l'" ///
    "(v[\`i']-1)<\`l'" "v[\`i']<10""
    doesn't work

    Code:
    local a1 "v[\`i']<\`l'" + ///
    "(v[\`i']-1)<\`l'" + ///
    "v[\`i']<10"
    works, but di "`a1'" fails, and most importantly, I couldn't "word 2 of `a1'" then use it as a condition in a if statement.

    Please help me understand how the parser works, and what would be the best approach to format this do file properly, my intend is to use "///" which I think is easier to read, also that I would prefer not to use such format, which is more difficult to read and (I think) cumbersome.

    Code:
    local a1 "v[\`i']<\`l'"
    local a1"`a1' (v[\`i']-1)<\`l'"
    local a1"`a1' v[\`i']<10"
    works
    Last edited by jerome falken; 06 May 2018, 10:46.

  • #2
    Here is one approach for the three slashes

    Code:
    loc i = 1
    loc 1  one
    local a1 v[\`i']<\`l'  ///
    (v[\`i']-1)<\`l' + ///
    v[\`i']<10
    di "`a1'"
    v[1]<  (v[1]-1)< + v[1]<10
    For your second line

    Code:
     loc i = 1
    loc 1  one
    local a1 "v[\`i']<\`l' + (v[\`i']-1)<\`l' + v[\`i']<10"
    di `"`a1'"'
    v[1]< + (v[1]-1)< + v[1]<10
    Last edited by Attaullah Shah; 06 May 2018, 11:24.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      I don't think this will work in my case because at the time the local is defined, nether value (v, i, l) is defined, which is why I kept things as strings.

      Comment


      • #4
        OK, I got it. So if you do not want your macro to expand, then use the slash (escape character) with the macro name as well. See for example

        Code:
        local a1 v[\`i']&lt;\`l'
        di "\`a1'"
        `a1'
        More on this here https://www.stata.com/support/faqs/p...es-and-macros/

        So when v, l, and i are not defined, do not expand it, once they are available, then expand

        Code:
        loc a v[\`i']<\`l' + (v[\`i']-1)<\`l' + v[\`i']<10
        
        dis "\`a'"
        `a'
        loc i = 5
        loc l = 6
        dis "`a'"
        v[5]<6 + (v[5]-1)<6 + v[5]<10
        Last edited by Attaullah Shah; 06 May 2018, 11:48.
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          Oh this is perfect, thank you!

          Comment


          • #6
            This kind of code is very hard to write and hard to read later even if you wrote it. It's next to impenetrable for anyone else to read, which may not matter if you're the only programmer in sight. My guess is that you are used to giving lots of definitions at the head of code as a matter of programming style other languages. I'd flag for you

            Code:
            help include
            as a way to put your definitions somewhere special and then use them when you want to.

            Comment


            • #7
              One more question Dr Shah,

              If I wanted to use globals so the scope could be visible from all functions (program), would I do this?

              Code:
              global g1 v[\`i']<\`l' ///
              (v[\`i']-1)<\`l' + ///
              v[\`i']<10
              di "`a1'"
              
              program def myprog
                local l1 "\$a1"
                [...] do great things [...]
              end

              Comment


              • #8
                If I have correctly understood your question ...
                Code:
                global g1 v[\`i']<\`l' ///
                (v[\`i']-1)<\`l' + ///
                v[\`i']<10
                
                cap prog drop myprog
                program def myprog
                  local l1 "\$g1"
                  loc l = 5
                  loc i =6
                  di "`l1'"
                end
                myprog
                v[6]<5 (v[6]-1)<5 + v[6]<10
                Regards
                --------------------------------------------------
                Attaullah Shah, PhD.
                Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                FinTechProfessor.com
                https://asdocx.com
                Check out my asdoc program, which sends outputs to MS Word.
                For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                Comment


                • #9
                  Thank you Dr Shah

                  Comment

                  Working...
                  X