Announcement

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

  • Breaking long lines inside strings

    Colleagues,

    I would like to ask whether it's possible to break the line inside the string as in the following code

    Code:
    foreach var of make - foreign {
        label variable `var' `"This is a very long label that takes some ///
            info from the variable and introduces it `=substr("`var'", 2, 1)' "'
        notes `var' : TS Imported from some data source
        }
    Obviously the /// sign is invalid and will cause the code to break but ideally I would like to do something on those lines as I have to handle very long labels.
    Last edited by Konrad Zdeb; 24 Jun 2014, 10:14.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    Hi Konrad,
    I've always used "///" to start a new line in my dofiles and it always works for me. However, it is a hassle to have to keep typing out /// and it never looks neat because you can never line them up perfectly. I suggest you automatically wrap your long labels. There is a post on how to do that below. Good luck and I hope this helps.
    -Diana

    <http://www.statalist.org/forums/forum/general-stata-discussion/general/29759-automatically-wrapping-long-labels>
    Last edited by Diana Rodriguez; 24 Jun 2014, 10:32. Reason: I'm not sure how to tag another post to this...since the url just takes you to the main page. I guess I have to visit the Statalist FAQ....

    Comment


    • #3
      Diana, you can break the code with /// but not the string constants that Konrad is asking about.

      Code:
      display "This is a ///
               long line"
      Konrad, usually it is convenient to introduce intermediate constants:
      Code:
      sysuse auto
      
      // note the total 80 chars limit on variable labels
      local vltmpl "This is a very long variable label of "
       
      foreach var of varlist * {
          local vl `"`=substr("`var'", 2, 1)'"'
          label variable `var' `"`vltmpl'`vl'"'
      }
      
      describe
      Not sure why you needed the substr() there in the variable label, but whatever. Keep each string short, combine when necessary. In mata use sprintf().

      Best, Sergiy Radyakin

      Comment


      • #4
        In addition, note that the following are also available when working with long strings in macros or notes:
        Code:
        1 loc foo Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ///
        2     eiusmod tempor incididunt ut labore et dolore magna aliqua.
        3
        4 loc foo = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do " ///
        5     + "eiusmod tempor incididunt ut labore et dolore magna aliqua."
        6
        7 note: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ///
        8     eiusmod tempor incididunt ut labore et dolore magna aliqua.
        The downside to 1–2 is that the leading spaces in line 2 will appear in the resulting macro, though this isn't a problem if what you're storing in the macro is essentially a list; if necessary, you can always avoid this by excluding the leading spaces (which makes the code less readable) or stripping them out afterward with something like : list retokenize. Note that 4–5 used to be limited by the maximum size of a string in a string expression, but no longer. Finally, 7–8 will automatically strip out the leading spaces on line 8 (a nice feature of note).
        Last edited by Phil Schumm; 24 Jun 2014, 13:01.

        Comment


        • #5
          Thank you very much for useful suggestions. I very much like Phil's suggestion with "+" as similar things can be done in R.
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment

          Working...
          X