Announcement

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

  • Extracting the first part of a string occurring before the first comma

    Hello

    I have data as below:

    Code:
    clear
    inp str199 precinct
    "North Allowton North, North Allowton Elementary, 1139 Corner Rd, Linville, PA 19333"
    "LN 2, Township Building, 3298 Center Dr, Uppertown, PA"
    "South 1 & 2, Dallingford Elementary, 422 S Long St, Chesterfield, PA"
    end
    I'm trying to separate and create a new variable from the first part of the string before the first comma, so:

    North Allowton North
    LN 2
    South 1 & 2

    I found a post (https://www.statalist.org/forums/for...=1729853922649) that seeks to get the last part of the string after the comma, but I couldn't figure out how to adapt that for my purposes.

    Any help would be greatly appreciated!

    Thanks,
    Meghan

  • #2
    Code:
    . split precinct, parse(",") generate(part)
    variables created as string:
    part1  part2  part3  part4  part5
    
    . list
    
         +------------------------------------------------------------------------------------------------------------+
      1. |                                                                            precinct |                part1 |
         | North Allowton North, North Allowton Elementary, 1139 Corner Rd, Linville, PA 19333 | North Allowton North |
         |------------------------------------------------------------------------------------------------------------|
         |                          part2     |               part3     |             part4      |         part5      |
         |      North Allowton Elementary     |      1139 Corner Rd     |          Linville      |      PA 19333      |
         +------------------------------------------------------------------------------------------------------------+
    
         +------------------------------------------------------------------------------------------------------------+
      2. |                                                                            precinct |                part1 |
         |                              LN 2, Township Building, 3298 Center Dr, Uppertown, PA |                 LN 2 |
         |------------------------------------------------------------------------------------------------------------|
         |                          part2     |               part3     |             part4      |         part5      |
         |              Township Building     |      3298 Center Dr     |         Uppertown      |            PA      |
         +------------------------------------------------------------------------------------------------------------+
    
         +------------------------------------------------------------------------------------------------------------+
      3. |                                                                            precinct |                part1 |
         |                South 1 & 2, Dallingford Elementary, 422 S Long St, Chesterfield, PA |          South 1 & 2 |
         |------------------------------------------------------------------------------------------------------------|
         |                          part2     |               part3     |             part4      |         part5      |
         |         Dallingford Elementary     |       422 S Long St     |      Chesterfield      |            PA      |
         +------------------------------------------------------------------------------------------------------------+
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Wow, way easier than what I was trying - thanks Maarten!

      Comment


      • #4
        Note that split allows an option limit().

        Also

        Code:
        gen wanted = substr(precinct, 1, strpos(precinct, ",") - 1)

        Comment

        Working...
        X