Announcement

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

  • How to replace the newline/nextline character in a column with space character

    Hello!

    I understand that subinstr can be used to replace a substring in a column
    https://www.stata.com/statalist/arch.../msg00386.html
    Code:
    gen newvar = subinstr(oldvar,"dis","reg",.)
    I would like to replace all appearances of newline, i.e. cr/lf - char(10) or char(13) - in a column with the space character. I used
    Code:
    gen newvar = subinstr(oldvar,"`=char(10)'"," ",.)
    gen newvar2 = subinstr(newvar,"`=char(13)'"," ",.)
    Is this the best way of doing this? Is there a more efficient, single line command?

    Also when thinking about next line character, are carriage return and line feed the only two I need to worry about, or are there other characters I am missing?
    Thank you for your help!

    Stata SE/17.0, Windows 10 Enterprise

  • #2
    You can imbed subinstr within subinstr
    HTML Code:
    https://www.statalist.org/forums/forum/general-stata-discussion/general/1455172-substr-within-a-subinstr
    ]

    Comment


    • #3
      try this.

      Code:
       
       gen newvar2 = subinstr(subinstr(oldvar,"`=char(10)'"," ",.),"`=char(13)'"," ",.)

      Comment


      • #4
        Nice, thanks, and chat(10) and char(13) are the only 2 I should worry about, correct?
        Thank you for your help!

        Stata SE/17.0, Windows 10 Enterprise

        Comment


        • #5
          I have no idea what you are up to aside from string replacement.

          Comment


          • #6
            Code:
            gen v2 = ustrregexra(v1, "\p{Space_Separator}", char(32))

            Comment


            • #7
              Originally posted by Bjarte Aagnes View Post
              Code:
              gen v2 = ustrregexra(v1, "\p{Space_Separator}", char(32))
              Nice! I should do that...
              Thank you for your help!

              Stata SE/17.0, Windows 10 Enterprise

              Comment

              Working...
              X