Announcement

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

  • How to combine 2 numerical variables and 1 string variable into ONE variable?

    Hello,

    I'm trying to combine 3 variables (2: numeric, 1: string) into one variable, and I'd appreciate if you help me how to do it.

    A particular multiple-choice questionnaire I'm dealing with is "Circle all that apply." Interviewers could circle more than one answers from participants. There is no first and second choice because interviewers marked their answers based on what they said. So I'd like to consider all answers independently when the participants picked two answers. For example,

    ID Answer1 Answer2 Answer3 Other (explain)
    1 1 2 . .
    2 3 . . explained in English
    3 3 . . .
    4 2 3 . .
    5 3 . . explained in English

    From this table, the total number of IDs (participants) is 5, but the total number of answers is 9. I'd want to make the new variable that has 9 rows.

    Thank you!

  • #2
    You need to provide more information. At the simplest level

    Code:
    egen combined_answer = concat(Answer1 Answer2 Other), punct(,)
    will work. But, for example, it treats Answer1 = 1, Answer2 =2 as being different from Answer2 = 1 and Answer1 = 2. Is that what you want? Or should those come out as the same thing.

    In the future, please do not post HTML tables to show example data. Also, example data should come from Stata--I'm sure yours didn't because Other (explain) is not a legal Stata variable name. The helpful way to show example data is with the -dataex- command. See FAQ #12 for instructions how to get -dataex- and other excellent advice on the helpful way to show code and Stata output in your future posts.

    Comment


    • #3
      Thank you very much for your answer, Professor Schechter!

      I used dataex and the example is as follows:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(answer1 answer2)
      3 .
      3 .
      3 .
      3 .
      3 .
      4 .
      3 .
      1 .
      1 .
      2 .
      1 1
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      1 .
      . .
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      4 .
      3 .
      5 .
      3 .
      5 .
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      2 .
      2 .
      3 2
      3 .
      3 .
      3 .
      4 .
      3 .
      3 .
      3 .
      4 .
      3 .
      3 .
      3 .
      3 .
      3 .
      5 .
      3 .
      3 .
      5 .
      2 .
      2 .
      5 .
      5 .
      5 .
      2 .
      3 .
      3 .
      3 .
      2 .
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      4 .
      4 .
      3 .
      . .
      4 1
      4 .
      3 2
      3 .
      5 .
      2 .
      4 .
      4 .
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      3 .
      4 1
      3 .
      . .
      3 .
      3 .
      3 .
      end

      What I'd like to see is that "Answer 2" column is located right below the "Answer 1" column to treat them as another IDs. I would count the number of participants based on the number of their answers. For example, if all 100 participants answered two of multiple choices, a total of participants would be 200.

      I also tried to add answer3, but it is string and I haven't found how to display them with keeping confidentiality. Likewise, "Answer 3" should be also concatenated in the first column to treat them as another IDs.

      What Stata command will align all three variables into one column? Thank you!

      Comment


      • #4
        Stata is not a spreadsheet and it doesn't have columns. It has variables. It is important not to think about Stata as if it were a spreadsheet because your habits and instincts from using spreadsheets are usually not helpful in Stata and often will mislead you altogether. So it is important not to use spreadsheet vocabulary when talking about Stata.

        You can't combine numeric and string variables into a single variable in Stata. (This is a good example of how Stata variables are different from spreadsheet columns.) The closest you can come to that is to convert the numeric variables to string, and then you can combine them. For example

        Code:
        tostring answer1 answer2, replace
        gen long obs_no = _n
        reshape long answer, i(obs_no) j(original)

        Comment


        • #5
          Thank you so much, Professor Schechter! I appreciate it.

          Comment

          Working...
          X