Announcement

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

  • Substring

    I have 8 string variables. How do I go from:
    Frank-Jim - "" - Tom-Tom- Anna-Jake-Jake

    To:
    Frank-Jim-Tom-Anna-Jake

    I want to remove the same names which are just beside each other (Tom and Jake in this case) and also the empty cells (""). Which code can I use?

    I have no dataset. Thanks in advance.

  • #2
    Here are some ideas for the dataset you don't have (???).

    Code:
    clear
    input str80 problem
    `"Frank-Jim - "" - Tom-Tom- Anna-Jake-Jake"'
    end
    
    split problem, parse("-")
    
    gen solution = problem1 if problem1 != `""""'
    forval j = 2/`r(k_new)' {
    replace solution = solution + "-" + trim(problem`j') if trim(problem`j') != `""""' & strpos(solution, trim(problem`j')) == 0
    }
    
    list problem solution
    
         +--------------------------------------------------------------------+
         |                                  problem                  solution |
         |--------------------------------------------------------------------|
      1. | Frank-Jim - "" - Tom-Tom- Anna-Jake-Jake   Frank-Jim-Tom-Anna-Jake |
         +--------------------------------------------------------------------+

    Comment

    Working...
    X