Announcement

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

  • Splitting a string variable

    Hello,

    I want to split a string variable such as "12.619.3" but only want to split it into two such as "12.619" and "3".
    Suggestion, please.
    Thanks,
    KH

  • #2
    With but one example of your data and no real description, it's difficult to guess what the pattern is.

    Which of these is correct:
    • You have a string variable consisting of only the digits 0-9 and periods - no letters, no signs, no commas. There are exactly two periods and you want to split it into two parts at the second period.
    • You have a string variable consisting of only the digits 0-9 and periods - no letters, no signs, no commas. There is at least one period and you want to split it into two parts at the final period.
    • Something else.

    Comment


    • #3
      If either of the two guesses in post #2 is correct, then the following example may start you in a useful direction.
      Code:
      clear all
      cls
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str10 have
      "12.169.3.4"
      "12.619.3"  
      "12.619"    
      "12."
      "12"
      "foo.bar" 
      "123,345.7"       
      end
      
      generate want1 = have
      generate want2 = ""
      replace want1 = ustrregexs(1) if ustrregexm(have,"(^[\d\.]*)\.(\d*$)")
      replace want2 = ustrregexs(2) if ustrregexm(have,"(^[\d\.]*)\.(\d*$)")
      list, clean
      Code:
      . list, clean
      
                   have       want1   want2  
        1.   12.169.3.4    12.169.3       4  
        2.     12.619.3      12.619       3  
        3.       12.619          12     619  
        4.          12.          12          
        5.           12          12          
        6.      foo.bar     foo.bar          
        7.    123,345.7   123,345.7          
      
      .

      Comment

      Working...
      X