Announcement

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

  • How to seperate numbers from a string variable

    I have a string variable that has labels and a number. I only need the number. How do I separate it out? This is an example.

    Code:
    clear
    input str30 IfindImmunologicalconceptsdi
    "(4) Agree"                     
    "(4) Agree"                     
    "(5) Strongly Agree"            
    "(2) Disagree"                  
    "(4) Agree"                     
    "(2) Disagree"                  
    "(4) Agree"                     
    "(2) Disagree"                  
    "(5) Strongly Agree"            
    "(4) Agree"                     
    "(3) Neither Agree nor Disagree"
    "(3) Neither Agree nor Disagree"
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(3) Neither Agree nor Disagree"
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(2) Disagree"                  
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(4) Agree"                     
    "(2) Disagree"                  
    "(2) Disagree"                  
    "(3) Neither Agree nor Disagree"
    "(3) Neither Agree nor Disagree"
    "(3) Neither Agree nor Disagree"
    "(3) Neither Agree nor Disagree"
    "(4) Agree"                     
    "(4) Agree"                     
    "(3) Neither Agree nor Disagree"
    "(2) Disagree"                  
    "(4) Agree"                     
    "(4) Agree"                     
    "(3) Neither Agree nor Disagree"
    "(4) Agree"                     
    "(4) Agree"                     
    "(2) Disagree"                  
    "(4) Agree"                     
    "(3) Neither Agree nor Disagree"
    "(3) Neither Agree nor Disagree"
    "(3) Neither Agree nor Disagree"
    "(4) Agree"                     
    "(2) Disagree"                  
    "(4) Agree"                     
    end



  • #2
    If they are all one digit numbers, then substr() would be fine. If there could be more, then regular expression would be more flexible:

    Code:
    clear
    input str30 IfindImmunologicalconceptsdi
    "(4) Agree"                    
    "(4) Agree"                    
    "(5) Strongly Agree"            
    "(2) Disagree"                  
    "(4) Agree"                    
    "(99) Something Else"    
    end
    
    * Method 1: Substring
    generate wanted1 = real(substr(IfindImmunologicalconceptsdi, 2, 1))
    
    * Method 2: Regular expression
    generate wanted2 = real(regexs(0)) if(regexm(IfindImmunologicalconceptsdi, "[0-9]+"))
    Result:

    Code:
         +-----------------------------------------+
         | IfindImmunologica~i   wanted1   wanted2 |
         |-----------------------------------------|
      1. |           (4) Agree         4         4 |
      2. |           (4) Agree         4         4 |
      3. |  (5) Strongly Agree         5         5 |
      4. |        (2) Disagree         2         2 |
      5. |           (4) Agree         4         4 |
      6. | (99) Something Else         9        99 |
         +-----------------------------------------+

    Comment


    • #3
      Consider also

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str30 Immconceptsdifficult
      "(4) Agree"                     
      "(5) Strongly Agree"            
      "(2) Disagree"                  
      "(3) Neither Agree nor Disagree"
      end
      
      . split Imm, parse(")")
      variables created as string: 
      Immconcept~1  Immconcept~2
      
      . l
      
           +-------------------------------------------------------------------------+
           |           Immconceptsdifficult   Immcon~1         Immconceptsdifficult2 |
           |-------------------------------------------------------------------------|
        1. |                      (4) Agree         (4                         Agree |
        2. |             (5) Strongly Agree         (5                Strongly Agree |
        3. |                   (2) Disagree         (2                      Disagree |
        4. | (3) Neither Agree nor Disagree         (3    Neither Agree nor Disagree |
           +-------------------------------------------------------------------------+
      
      . destring Imm*1, ignore("(") replace
      Immconceptsdifficult1: character ( removed; replaced as byte
      
      . l
      
           +-------------------------------------------------------------------------+
           |           Immconceptsdifficult   Immcon~1         Immconceptsdifficult2 |
           |-------------------------------------------------------------------------|
        1. |                      (4) Agree          4                         Agree |
        2. |             (5) Strongly Agree          5                Strongly Agree |
        3. |                   (2) Disagree          2                      Disagree |
        4. | (3) Neither Agree nor Disagree          3    Neither Agree nor Disagree |
           +-------------------------------------------------------------------------+
      
      .

      Comment


      • #4
        Thank you, Ken and Nick for the solutions.

        Regards,
        Al

        Comment

        Working...
        X