Announcement

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

  • Remove part of string variable and format variable

    Hi. I am trying to do the following with my string variable:

    1. remove the "-" and everything before it in my string variable. So "HEI-GMRV" will be "GMRV"
    2. remove all lead and lag spaces in what is left after #1. So, "HEI - Kadapa" should be only "Kadapa"

    I know how to replace individual parts of the string, but not how to "remove all parts before "-"". Any help in doing this would be very much appreciated.

    Code:
    input str13 doc_location
    "HEI-GMRV"     
    "HEI-GPR ICARE"
    "HEI-KVC"      
    "HEI-KAR"      
    "HEI-KAR"      
    "HERF-KAR"      
    "HEI-KAR"      
    "HEI-GMRV"   
    "HEI - Kadapa"

  • #2
    Try something like the following.
    Code:
    generate str loc = strtrim(substr(doc_location, strpos(doc_location, "-")+1, .))

    Comment


    • #3
      You could use split but a direct approach is better:

      Code:
      clear 
      input str13 doc_location
      "HEI-GMRV"     
      "HEI-GPR ICARE"
      "HEI-KVC"      
      "HEI-KAR"      
      "HEI-KAR"      
      "HERF-KAR"      
      "HEI-KAR"      
      "HEI-GMRV"   
      "HEI - Kadapa"
      end 
      
      gen wanted = trim(substr(doc_location, 1 + strpos(doc_location, "-"), .))

      Comment


      • #4
        See https://journals.sagepub.com/doi/pdf...867X1101100308 for a review of basic functions, especially Section 5.

        Note that the function names given there still work. So strtrim() as used in #2 and trim() as used in #3 and as discussed in that paper are one and the same.

        Comment


        • #5
          Nick Cox: Thanks. That was really helpful

          Comment

          Working...
          X