Announcement

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

  • String Variables

    Hello,

    I recently conducted a survey via Qualtrics for my thesis. When I downloaded the file and imported the data into Stata I realized the questions that allowed for multiple responses were formatted as string variables. The data in each cell has entries that look like "1,2,3,4,5,6" to indicate the respondent checked those boxes. Is there a way to break up the string variable so I can see all the respondents who checked 1, those who checked 2, etc.?

  • #2
    You can scan for each of the numerals in a loop and generate an indicator variable for its presence. I think that Nick Cox has the best approach whenever a question like yours has come up on the list before, and if I recall correctly his looks something like this.
    Code:
    replace qualtrics = " " + subinstr(qualtrics, ",", " ", .) + " "
    forvalues i = 1/6 {
        generate byte chose`i' = strpos(qualtrics, " `i' ") > 0
    }

    Comment


    • #3
      I doubt that I invented this but thanks to Joseph for the mention. What can bites is any use of integers greater than 9 so that say a search for "1" alone must not catch its occurrence in 10, 11, 12, ..., 19, 21, ...., 100, 101, 102, ....

      Comment


      • #4
        Below code, which might be less blemish and more general, is more like Nick's. Many thanks to Nick Cox for all of your tricky lessons.
        Code:
        clear
        input float id str11 qa
        1 "1,2,3,4,5,6,7,8,9,10,11,12,13"
        2 "1,7,4,12"      
        3 "12,2,3,2"      
        4 "1,4,5,6,1,11"  
        end
        
        split qa, p(,) gen(c)
        reshape long c, i(id)
        tab c, gen(check)
        mvdecode check*, mv(0=.)
        collapse (firstnm) qa check*, by(id)
        Last edited by Romalpa Akzo; 21 Jul 2018, 05:11.

        Comment


        • #5
          Thank you for your responses Joseph Coveney Romalpa Akzo and @ Nick Cox. Can someone maybe explain to me how the code works? I'm still pretty confused and haven't resolved my issue.

          Comment

          Working...
          X