Announcement

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

  • Adding spaces to postcodes

    Dear Statalist

    I have a postcode string variable which I need to map to another geographical variable (e.g local authority, country region etc). To map to the "using" dataset, the postcode variable needs to have a space after the third character. Some of the observations do not have this - is there a way of introducing it to all observations in this variable?

    e.g
    Post code in the desired format:
    ABC DEF

    Some observations have the postcode entered as:
    ABCDEF

    I need to add a space after the third character (for 15,000 observations) , or it will not match with the other dataset I am trying to merge with

    Any help would be greatly appreciated!

    Thank you

    Joe

  • #2
    This sounds like a case of British postcodes. On an international list, explaining that if so does no harm. (It's far more common that members in the United States assume that everyone else is so located.)

    Either way

    Code:
    gen new = substr(old, 1, 3) + " " + substr(old, 4, 3) if length(old) == 6 
    replace new = old if missing(old)
    may help. Note that before that

    Code:
    replace old = trim(itrim(old))
    will catch painlessly any leading or trailing spaces or multiple spaces internally that should be one.

    My own home postcode is, naturally enough, "DH1 2NJ" showing that the system recognises individuals in some cases.

    Comment


    • #3
      Dear Nick

      Yes, I should have mentioned these are British postcodes. Than you very much for your help - it worked a treat!

      Best Wishes

      Joe

      Comment

      Working...
      X