Announcement

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

  • Making a string variable with multiple lines

    Hi there, I have a fairly straightforward question. I would like to create a set of emails in Stata, which need to span multiple lines. Does anyone know how to create a string variable that spans multiple lines, where I can determine which content goes in which line?

    For example, I would like a string that looked like this:

    "Hi Bob,

    How are you?

    Best, Nick"

    But I cant find anywhere how to construct this.

    Thanks!

  • #2
    You could just write that text in the Do-file editor and then copy and paste into the Editor.

    Comment


    • #3
      If you are running a Windows system, you have to bear in mind that Windows defines newlines as a sequence of two characters, CR and LF, which are ASCII characters 13 and 10, respectively. So you can do this:
      Code:
      . clear*
      
      . local newline `=char(10)'`=char(13)'
      
      .
      . set obs 1
      Number of observations (_N) was 0, now 1.
      
      . gen demo = "Hi, Bob`newline'How are you?`newline'Best, Nick"
      
      .
      . display demo[1]
      Hi, Bob
      How are you?
      Best, Nick
      That said, if you examine variable demo using -list- these newlines will appear only as spaces. And if you view the variable in the Browser, you will see it all run together, with nothing visible at all where you expect the newlines. I do not know what happens if you attempt to print these into hard copy--I've never tried it and don't have access to a printer at this time.

      Further complicating matters, if you are using Mac, then a newline is just ASCII 13. And in Unix it is just ASCII 10.

      Added: Crossed with #2. That approach, of course, does not lend itself to generating these in code.

      Correction: While what I said about OS expectations for newlines is correct, a little experimentation on my Windows system reveals that using either `=char(13)' or `=char(10)' alone will also produce the desired result with -display-.
      Last edited by Clyde Schechter; 20 Dec 2024, 10:11.

      Comment

      Working...
      X