Announcement

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

  • split a variable

    Hello!
    I have two string variables. The first men1743 relate to the climate risks that the household faces and the second men1745 represent the adaptation measures to these risks.
    I would like to transform these variables into numeric.
    For the variable men1743 for example we have
    1 =Drought
    2 =Floods
    3 =Insect pests
    4 = Wind erosion
    5 =Water erosion
    6 =Salinization
    7 =Other to be specified

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 men1743 str5 men1745
    "7"    "5"    
    "134"  "12345"
    "3"    "4"    
    "14"   "5"    
    ""     ""     
    "7"    "5"    
    "7"    "5"    
    "1235" "5"    
    "3"    "5"    
    "13"   "1234" 
    end
    ------------------ copy up to and including the previous line ------------------

    thank you in advance for your assistance

  • #2
    Perhaps this is a start at what you want?
    Code:
    forvalues i=1/7 {
        generate m3_`i' = strpos(men1743,"`i'")>0
    }
    list men1743 m3_*, clean
    forvalues i=1/5 {
        generate m5_`i' = strpos(men1745,"`i'")>0
    }
    list men1745 m5_*, clean
    Code:
    . forvalues i=1/7 {
      2.     generate m3_`i' = strpos(men1743,"`i'")>0
      3. }
    
    . list men1743 m3_*, clean
    
           men1743   m3_1   m3_2   m3_3   m3_4   m3_5   m3_6   m3_7  
      1.         7      0      0      0      0      0      0      1  
      2.       134      1      0      1      1      0      0      0  
      3.         3      0      0      1      0      0      0      0  
      4.        14      1      0      0      1      0      0      0  
      5.                0      0      0      0      0      0      0  
      6.         7      0      0      0      0      0      0      1  
      7.         7      0      0      0      0      0      0      1  
      8.      1235      1      1      1      0      1      0      0  
      9.         3      0      0      1      0      0      0      0  
     10.        13      1      0      1      0      0      0      0  
    
    . forvalues i=1/5 {
      2.     generate m5_`i' = strpos(men1745,"`i'")>0
      3. }
    
    . list men1745 m5_*, clean
    
           men1745   m5_1   m5_2   m5_3   m5_4   m5_5  
      1.         5      0      0      0      0      1  
      2.     12345      1      1      1      1      1  
      3.         4      0      0      0      1      0  
      4.         5      0      0      0      0      1  
      5.                0      0      0      0      0  
      6.         5      0      0      0      0      1  
      7.         5      0      0      0      0      1  
      8.         5      0      0      0      0      1  
      9.         5      0      0      0      0      1  
     10.      1234      1      1      1      1      0  
    
    .

    Comment


    • #3
      Thank you very much William.
      Best regards

      Comment

      Working...
      X