Announcement

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

  • List long string variable values in aesthetically appealing fashion

    Hi. My student has a string variable of type str2000. It is for people to add additional comments to a survey if they want to. Most write nothing, some write a few words, a few go on for several hundred words.

    What is an aesthetically appealing way to list these comments? If I use the list command, every comment (even it is only a few words) takes up several lines, and each line after the first starts with a >.

    I would like each comment to take up only as much space as is needed, and for long comments not to have > inserted on each line. A blank line after each comment would be nice too.

    I suspect there is an easy way to do this, but I've never worked with such long string variables before.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://academicweb.nd.edu/~rwilliam/

  • #2
    Here is a way to break up the text in XXX word chunks. I drop observations with zero words, so do not save the resulting output.

    Code:
    clear
    input float id str2000 comment
    1 "Hi. My student has a string variable of type str2000. It is for people to add additional comments to a survey if they want to. Most write nothing, some write a few words, a few go on for several hundred words."
    
    2 " "
    3 " "
    4 "What is an aesthetically appealing way to list these comments? If I use the list command, every comment (even it is only a few words) takes up several lines, and each line after the first starts with a >."
    5 "one two"
    6 ""
    7 "I would like each comment to take up only as much space as is needed, and for long comments not to have > inserted on each line. A blank line after each comment would be nice too."
    
    8 "I suspect there is an easy way to do this, but I've never worked with such long string variables before."
    
    9 "Hi. My student has a string variable of type str2000. It is for people to add additional comments to a survey if they want to. Most write nothing, some write a few words, a few go on for several hundred words. What is an aesthetically appealing way to list these comments? If I use the list command, every comment (even it is only a few words) takes up several lines, and each line after the first starts with a >. I would like each comment to take up only as much space as is needed, and for long comments not to have > inserted on each line. A blank line after each comment would be nice too. I suspect there is an easy way to do this, but I've never worked with such long string variables before."
    end
    
    replace comment= trim(itrim(comment))
    drop if length(comment)<1
    gen length= length(comment)
    gen toexpand= ceil(length/100)
    expand toexpand
    bys id: gen formatted= substr(comment, _n*100-99, 100) 
    list formatted, sepby(id)
    Res.:

    Code:
    list formatted, sepby(id)
    
         +------------------------------------------------------------------------------------------------------+
         |                                                                                            formatted |
         |------------------------------------------------------------------------------------------------------|
      1. | Hi. My student has a string variable of type str2000. It is for people to add additional comments to |
      2. |  a survey if they want to. Most write nothing, some write a few words, a few go on for several hundr |
      3. |                                                                                            ed words. |
         |------------------------------------------------------------------------------------------------------|
      4. | What is an aesthetically appealing way to list these comments? If I use the list command, every comm |
      5. | ent (even it is only a few words) takes up several lines, and each line after the first starts with  |
      6. |                                                                                                 a >. |
         |------------------------------------------------------------------------------------------------------|
      7. |                                                                                              one two |
         |------------------------------------------------------------------------------------------------------|
      8. | I would like each comment to take up only as much space as is needed, and for long comments not to h |
      9. |                      ave > inserted on each line. A blank line after each comment would be nice too. |
         |------------------------------------------------------------------------------------------------------|
     10. | I suspect there is an easy way to do this, but I've never worked with such long string variables bef |
     11. |                                                                                                 ore. |
         |------------------------------------------------------------------------------------------------------|
     12. | Hi. My student has a string variable of type str2000. It is for people to add additional comments to |
     13. |  a survey if they want to. Most write nothing, some write a few words, a few go on for several hundr |
     14. | ed words. What is an aesthetically appealing way to list these comments? If I use the list command,  |
     15. | every comment (even it is only a few words) takes up several lines, and each line after the first st |
     16. | arts with a >. I would like each comment to take up only as much space as is needed, and for long co |
     17. | mments not to have > inserted on each line. A blank line after each comment would be nice too. I sus |
     18. |  pect there is an easy way to do this, but I've never worked with such long string variables before. |
         +------------------------------------------------------------------------------------------------------+

    Comment


    • #3
      Code:
      forvalues i = 1/`c(N)' {
      
              di ustrregexra(comment[`i'], "(.{75,80}\b)", "$1\u000A") _n
      }

      Comment


      • #4
        Andrew and Bjarte, thanks! Both approaches look promising. I want to include the values of some of the other variables so I'll play around and see what gives the most aesthetically appealing results.
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        StataNow Version: 19.5 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://academicweb.nd.edu/~rwilliam/

        Comment


        • #5
          Thanks, for exploring data on screen you may consider mixing #2 or #3 with browse like:
          Code:
          prog define lbr
          cls 
          di ustrregexra(comment[`1'], "(.{75,80}\b)", "$1\u000A") _n 
          br id in `1'
          end
          which for a given record will display the long wrapped text in the result window, and browse some variables

          Comment

          Working...
          X