Announcement

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

  • How to generate dummy variable from a String variable

    Hello.
    I have a string variable which has multiple response such as "1,3", "6,96" etc. Now I want to create a dummy variable for each response. I have tried strops but it has problems for two or three-digit responses sucha s for "96" it gives values in 6 , 9 and 96. How to solve it?

    clear input str4 cards_hh "3" "1" "2" "1,2" "1" "4,96" "1,3" "96" "3" "1,3" "6,96" "2" "88" "1" "1,2,3,88" "4,96" "1" "2,3"

  • #2
    Thanks for your data example. It's contradictory as a value such as "1,2,3,88" could not fix inside a str4 variable! That may be one of your problems.

    I don't know what strops (or strops) means here.

    If it's a community-contributed command, you're asked to explain where it comes from (FAQ Advice #12).

    Perhaps you mean string operators; either way we can't explain what is problematic about code you don't show us.

    Here is some technique that may help.

    Code:
    clear 
    input str42 cards_hh 
    "3" 
    "1" 
    "2" 
    "1,2" 
    "1" 
    "4,96" 
    "1,3" 
    "96" 
    "3" 
    "1,3" 
    "6,96" 
    "2" 
    "88" 
    "1" 
    "1,2,3,88" 
    "4,96" 
    "1" 
    "2,3"
    end 
    
    split cards_hh, p(,) destring 
    
    local splitvars `r(varlist)'
    
    local levels 
    
    foreach v in `r(varlist)' { 
    
    levelsof `v', local(work) clean 
    
    local levels : list levels | work 
    
    } 
    
    local levels : list sort levels 
    
    foreach level of local levels { 
        gen is`level' = 0 
        foreach v of local splitvars { 
            replace is`level' = 1 if `v' == `level'
        }
    }
    
    list cards_hh is*
    
         +------------------------------------------------------+
         | cards_hh   is1   is2   is3   is4   is6   is88   is96 |
         |------------------------------------------------------|
      1. |        3     0     0     1     0     0      0      0 |
      2. |        1     1     0     0     0     0      0      0 |
      3. |        2     0     1     0     0     0      0      0 |
      4. |      1,2     1     1     0     0     0      0      0 |
      5. |        1     1     0     0     0     0      0      0 |
         |------------------------------------------------------|
      6. |     4,96     0     0     0     1     0      0      1 |
      7. |      1,3     1     0     1     0     0      0      0 |
      8. |       96     0     0     0     0     0      0      1 |
      9. |        3     0     0     1     0     0      0      0 |
     10. |      1,3     1     0     1     0     0      0      0 |
         |------------------------------------------------------|
     11. |     6,96     0     0     0     0     1      0      1 |
     12. |        2     0     1     0     0     0      0      0 |
     13. |       88     0     0     0     0     0      1      0 |
     14. |        1     1     0     0     0     0      0      0 |
     15. | 1,2,3,88     1     1     1     0     0      1      0 |
         |------------------------------------------------------|
     16. |     4,96     0     0     0     1     0      0      1 |
     17. |        1     1     0     0     0     0      0      0 |
     18. |      2,3     0     1     1     0     0      0      0 |
         +------------------------------------------------------+

    Comment


    • #3
      That works fine. Thank you.

      Comment

      Working...
      X