Announcement

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

  • remove strings from observations

    Hello,

    I have the following variables in string format:
    x1 x2
    aaa [1] ddd [xy]
    bbb [2] eee [1]
    ccc [3] fff [2]
    bbb [2] eee [1]
    aaa [1] ...

    How can I remove [...] (and the space) from each observation?
    Thanks

  • #2

    Code:
    replace x1 = substr(x1, 1, strpos(x1, " ") - 1)
    or

    Code:
    replace x1 = word(x1, 1) 
    would seem candidates here. In future, do please use dataex to give real(istic) examples (FAQ Advice #12). Schematic examples may omit details that could scupper suggested code.

    Comment


    • #3
      dear Nick, I apologise, you are totally right, there was an omission so that the code you provided cannot be applied here with the schematic example (one word of string). This is the actual case:


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str71 father_occ str56 ageinjapan
      "Farm laborers including foremen [6]" "Between ages 0-9 and also 10-19 [4]"
      "Unknown [&]"                         "Between ages 0-9 and also 10-19 [4]"
      "Service [4]"                         "Never in Japan [0]"                 
      "Farm operators and managers [5]"     "Between ages 0-9 and also 10-19 [4]"
      "Service [4]"                         "Never in Japan [0]"                 
      end

      I just added two variables but I have many other similar ones, I was thinking to loop with _all

      Comment


      • #4
        So, look for the left bracket. Here I use trim() rather than assume that there is always and only one space before the left bracket.

        Code:
         
         replace x1 = trim(substr(x1, 1, strpos(x1, "[") - 1))

        Comment


        • #5
          Thanks!

          Comment

          Working...
          X