Announcement

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

  • Splitting Multiple Response Variable

    I have a multiple response variable that may take any combination of responses between 1 to 5 and 97. For instance, the variable may have responses in any of the following (i) 1 only or (ii) 2 and 3, or (iii) 97 only, or (iv) 3, 5, and 97, or (v) 1, 2, 3, 4, 5, and 97, or (vi) any other possible combinations. The variable is in string form with each response separated by a space. I would like to split this variable into 6 different variables for each choices/responses. For instance variable_1, variable_2, ........., variable_97. For each of these individual variables, I want responses in 0 and 1 (true false) format. For example, if a response has values 3 and 5. I want value 1(or true) for variables "var_3" and "var_5" and value 0 (false) for the rest of the variables.
    Could anyone kindly suggest a way of splitting the variables in this way in STATA?
    Thanks.

  • #2
    Here's one way to do it.

    Code:
    clear
    input str42 myvar 
    "1" 
    "2 3" 
    "97"
    "3 5 97"
    "1 2 3 4 5 97"
    end 
    
    split myvar, destring 
    local results "`r(varlist)'"
    
    quietly foreach j in 1 2 3 4 5 97 { 
        gen is`j' = 0 
        foreach v of local results { 
            replace is`j' = 1 if `v' == `j'
        }
    } 
    
    list myvar is* 
     
         +---------------------------------------------------+
         |        myvar   is1   is2   is3   is4   is5   is97 |
         |---------------------------------------------------|
      1. |            1     1     0     0     0     0      0 |
      2. |          2 3     0     1     1     0     0      0 |
      3. |           97     0     0     0     0     0      1 |
      4. |       3 5 97     0     0     1     0     1      1 |
      5. | 1 2 3 4 5 97     1     1     1     1     1      1 |
         +---------------------------------------------------+
    And here is another way, simpler in this case. (However, looking for 1 when say 11 is a possibility is trickier. See e.g. https://journals.sagepub.com/doi/pdf...6867X221141068 on what to do instead.)


    Code:
     
    foreach v in 1 2 3 4 5 97  { 
        gen IS`v' = strpos(myvar, "`v'") > 0 
        assert IS`v' == is`v' 
    }
    Note also egen's anymatch() fiunction, which is just the first method in different form.

    Comment


    • #3
      Thank you Nick Cox !

      Comment

      Working...
      X