Announcement

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

  • Shortening a string

    Hello, I have a variable which stores long numeric strings. I want to convert it into a shorter string with only its unique characters. For e.g. my longer string contains 0000002222221111111 and I want to create a new variable that stores only 021. Could someone please help me with how to code this?

  • #2
    It's a bit blunt-force and probably could be made more efficient, but maybe something like the following. (Begin at the "Begin here" comment.)
    Code:
    version 18.0
    
    clear *
    
    input str244 a
    "0000002222221111111"
    end
    
    *
    * Begin here
    *
    assert !indexnot(a, "0123456789")
    
    generate str b = substr(a, 1, 1)
    replace a = subinstr(a, b, "", .)
    
    local p 1
    while strlen(a) > 0 {
    
        local ++p
        tempvar `p'
        local addlist `addlist' ``p''
    
        generate str ``p'' = substr(a, 1, 1)
        replace a = subinstr(a, ``p'', "", .)
    }
    
    foreach var of varlist `addlist' {
        replace b = b + `var'
    }
    
    list b, noobs
    
    exit
    It preserves the order of the numerals' first appearances that you seem to want. It will be shorter if you just wanted the numerals in natural sequence.

    Comment


    • #3
      You don't list any specifications about the task, but if for example your numeric string has various lengths, then you'll need to add some amour plate, such as the following.
      Code:
      version 18.0
      
      clear *
      
      input str244 a
      "2"
      "0000002222221111111"
      "1111111111111111111"
      ""
      end
      
      *
      * Begin here
      *
      assert !indexnot(a, "0123456789")
      
      generate str b = substr(a, 1, 1)
      replace a = subinstr(a, b, "", .)
      
      local p 1
      generate int len = strlen(a)
      summarize len, meanonly
      while r(max) > 0 {
      
          local ++p
          tempvar `p'
          local addlist `addlist' ``p''
      
          generate str ``p'' = substr(a, 1, 1)
          replace a = subinstr(a, ``p'', "", .)
      
          replace len = strlen(a)
          summarize len, meanonly
      }
      
      foreach var of varlist `addlist' {
          replace b = b + `var'
      }
      
      list b, noobs
      
      exit

      Comment

      Working...
      X