Announcement

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

  • Dropping last characters

    Dear Stata users,

    I am trying to homogenize a variable (Zip code) format.
    Here is the example:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str20 addzip
    ""          
    ""          
    ""          
    ""          
    ""          
    ""          
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "60191"     
    "02865"     
    "02865"     
    "02865"     
    "02865"     
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    "55344-2252"
    end
    I would like to make this variable in (first) 5 characters, in other words, drop all the characters after -.
    Could someone please help me dealing with this issue ?

    Thank you very much in advance !

    best,
    AC

  • #2
    Well, for US zip codes, as they are all 5 digits, plus the optional - and 4 digits, you just need the first five characters:

    Code:
    gen short_ship = substr(addzip, 1, 5)

    Comment


    • #3
      Thank you very much Clyde ! It works well

      Comment

      Working...
      X