Announcement

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

  • How to order within a string containing letters and numbers?

    Dear all,

    I have a string variable, it looks like:

    A1B5B3H7D2

    Now I need to order it by every two digits. The desirable results will be:

    A1B3B5D2H7

    Thanks a lot!

  • #2
    You might want to start by Googling first normal form.

    Comment


    • #3
      This works with your example:

      Code:
      clear 
      input str8 whatever 
      "A1B5B3H7D2"
      end 
      
      forval j = 1/4 { 
          gen  w`j' = substr(whatever, `j' * 2 - 1, 2) 
      } 
      
      * install from Stata Journal 
      rowsort w?, generate(W1 W2 W3 W4) 
      egen WHATEVER = concat(W?) 
      
      list 
      
           +-------------------------------------------------------------+
           | whatever   w1   w2   w3   w4   W1   W2   W3   W4   WHATEVER |
           |-------------------------------------------------------------|
        1. | A1B5B3H7   A1   B5   B3   H7   A1   B3   B5   H7   A1B3B5H7 |
           +-------------------------------------------------------------+

      Comment


      • #4
        Originally posted by Nick Cox View Post
        This works with your example:

        Code:
        clear
        input str8 whatever
        "A1B5B3H7D2"
        end
        
        forval j = 1/4 {
        gen w`j' = substr(whatever, `j' * 2 - 1, 2)
        }
        
        * install from Stata Journal
        rowsort w?, generate(W1 W2 W3 W4)
        egen WHATEVER = concat(W?)
        
        list
        
        +-------------------------------------------------------------+
        | whatever w1 w2 w3 w4 W1 W2 W3 W4 WHATEVER |
        |-------------------------------------------------------------|
        1. | A1B5B3H7 A1 B5 B3 H7 A1 B3 B5 H7 A1B3B5H7 |
        +-------------------------------------------------------------+
        Thanks a ton, Nick. That helps!

        Comment

        Working...
        X