Announcement

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

  • generating a dummy variable if another variable contains an exact number

    Hello, I want to create a dummy variable. If funback contains 8 or 7 generate Hist=1.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 Stkcd str23 funback
    "000001" "4,5,7,8"  
    "000001" "5,6,7,8"  
    "000001" "5,7"      
    "000001" "5,7"      
    "000001" "7"        
    "000001" "7,9"      
    "000001" "4,5,6,7,8"
    "000001" "5,6,7,8"  
    "000001" "4,5,7"    
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "5,6,7,8"  
    "000001" "2,5,7"    
    "000001" "4,5,6,7"  
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "5,6,7"    
    "000001" "5,7,8"    
    "000001" "4,5,7,8"  
    "000001" "5,6,7"    
    "000001" "5,6,7"    
    "000001" "2,5,7,8"  
    "000001" "5,7,9"    
    "000001" "5,6,7"    
    "000001" "5,6,7"    
    "000001" "7,8"      
    "000001" "7,9"      
    "000001" "4,5,6,7,8"
    "000001" "5,6,7,8"  
    "000001" "4,5,7"    
    "000001" "5,7,8"    
    "000001" "5,6,7"    
    "000001" "2,5,7"    
    "000001" "4,5,6,7"  
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "5,6,7"    
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "4,5,7,8"  
    "000001" "5,6,7"    
    "000001" "5,6,7"    
    "000001" "2,5,7,8"  
    "000001" "5,6,7"    
    "000001" "5,6,7"    
    "000001" "7,8"      
    "000001" "7,9"      
    "000001" "4,5,6,7,8"
    "000001" "5,6,7,8"  
    "000001" "4,5,7"    
    "000001" "4,5,7,8"  
    "000001" "5,7,8"    
    "000001" "5,6,7"    
    "000001" "2,5,7"    
    "000001" "4,5,6,7"  
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "5,6,7"    
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "4,5,7,8"  
    "000001" "5,6,7"    
    "000001" "5,6,7"    
    "000001" "5,7,8"    
    "000001" "5,6,7"    
    "000001" "5,6,7"    
    "000001" "7,8"      
    "000001" "7,9"      
    "000001" "4,5,6,7"  
    "000001" "5,6,7,8"  
    "000001" "4,5,7"    
    "000001" "4,5,7,8"  
    "000001" "5,7"      
    "000001" "5,6,7"    
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "4,5,6,7"  
    "000001" "5,7,8"    
    "000001" "5,6,7"    
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "5,7"      
    "000001" "4,5,7,8"  
    "000001" "5,7"      
    "000001" "5,6,7"    
    "000001" "6,7"      
    "000001" "5,6,7"    
    "000001" "5,6,7"    
    "000001" "7,8"      
    "000001" "7,9"      
    "000001" "4,5,6,7"  
    "000001" "4,5,7"    
    "000001" "4,5,7,8"  
    "000001" "5,7,8"    
    "000001" "5,6,7"    
    "000001" "5,7,8"    
    "000001" "5,7,8"    
    "000001" "4,5,6,7"  
    "000001" "5,7,9"    
    end

  • #2
    Code:
    generate byte Hist = strpos(funback, "8") > 0 | strpos(funback, "7") > 0 // if !mi(funback)
    Or perhaps you could use a regular expression.

    Comment


    • #3
      Watch out for numbers that contain 7 or 8, e.g., 17, 71, 18, and 81, which might require slightly more complex matching patterns.

      Comment


      • #4
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str10 Stkcd str23 funback
        "000001" "4,5,7,8"  
        "000001" "17,71,18,81"  
        "000001" "7,71" 
        "000001" "88,77"   
        "000001" "5,8"            
        end
        
        gen wanted= ustrregexm(" "+funback+ " ", "[\, ](7|8)[\, ]")
        l
        Res.:

        Code:
        . l
        
             +-------------------------------+
             |  Stkcd       funback   wanted |
             |-------------------------------|
          1. | 000001       4,5,7,8        1 |
          2. | 000001   17,71,18,81        0 |
          3. | 000001          7,71        1 |
          4. | 000001         88,77        0 |
          5. | 000001           5,8        1 |
             +-------------------------------+

        Comment

        Working...
        X