Announcement

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

  • Splitting strings over multiple lines in a do file

    Dear all,

    I would like to do the following in a do-file:

    Code:
    label var x "1234567\\\
                    8901011"
    So I would like to split the string over multiple lines using the line-join operator. However it does not seem to work. If I use /* and */ it does. Could someone maybe explain the correct method of splitting a long string over multiple lines?

    Thanks!

  • #2
    us forward slashes (/) not the backward slashes you have above

    Comment


    • #3
      edit: forgot to include: see -h comment-

      Comment


      • #4
        Dear Rich, it does not work . The error I got is: "unrecognized command: the", the second line starts with the but that doesn't matter.

        Comment


        • #5
          Rich is right that you need to be using /// to continue a command onto the next line. However, that doesn't help in your case since you're trying to break in the middle of a string, which doesn't work with either of the ways of using comments to break a long line. Using /* and */ won't generate an error, but you also won't get the second line of text included in the label.

          You probably want to switch your delimiter for this command
          Code:
          #delimit ;
          label var x "This is a long label
               with more text on second line" ;
          #delimit cr
          desc x

          Comment


          • #6
            If you have very many of them, might be simplest to use whatever is the source of the strings, and then, assuming there is an ID variable, merge them on. Sarah's suggestion goes straight to the heart of the matter (takes you right to labels) and is a good one, but if you have very many to deal with, merging might be simpler in the long run.

            Comment


            • #7
              Dear Sarah and Ben,

              I was afraid of that I have to use delimiters, since I pretty much hate them. Well, thank you for your confirm that it will never work to break a string into two lines using the line-join. In this case your (Sarah) solution is pretty good since it fixes the delimiter to a certain area. Thanks!

              Comment


              • #8
                Originally posted by bsc.j.j.w View Post
                I was afraid of that I have to use delimiters, since I pretty much hate them.
                So do I, but what I dislike even more is switching between delimiters within a given file. One alternative is to put all of your variable labels in a do-file structured like this:
                Code:
                 1  loc make Lorem ipsum dolor sit "amet", consectetur adipisicing elit, sed ///
                 2      do eiusmod
                 3  loc price tempor incididunt ut labore et dolore magna aliqua. Ut enim ad ///
                 4      minim veniam
                 5  loc mpg quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea ///
                 6      commodo consequat.
                 7  
                 8  foreach var of varlist _all {
                 9      if `"``var''"'!="" {
                10          lab var `var' `"``var''"'
                11      }
                12  }
                (which you can run against the auto data if you want). In fact, you can even put the second block (lines 8–12) in a separate location (e.g., an ado-file) so that you can reuse it, which then makes the resulting do-file containing the labels even easier to read and maintain.

                Comment


                • #9
                  Along Phil's lines, note this technique

                  Code:
                   
                  local foo "start " 
                  local foo "`foo' middle "
                  local foo "`foo' end"
                  and this

                  Code:
                   
                  local foo "start " /// 
                  "middle " /// 
                  "end"

                  Comment


                  • #10
                    Ah, thank you! This all makes sense now!

                    Comment


                    • #11
                      Duplicate of this
                      http://www.statalist.org/forums/forum/general-stata-discussion/general/32138-breaking-long-lines-inside-strings


                      Intermediate constants and string concatenation are two most common suggestions. Others are possible depending on situation.
                      Best, Sergiy Radyakin

                      Comment


                      • #12
                        How can something so simple and useful be that complicated??? I think this is just another proof that STATA is an ill-designed language...

                        Comment

                        Working...
                        X