-1

I am currently cleaning a dataset with postcodes to Geocode them into ArcMAP.

Currently, the postcodes have been cleaned to remove all blank spaces (leading, trailing, in between, etc,). What I would like is to insert a blank space before the last three characters and am unsure how to do this. It is a string variable.

gen str8=postcodenew=subinstr(postcode," ", "", .)

I have done this to remove spaces but now want to enter one in three characters from last character.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Exodia16
  • 177
  • 2
  • 6
  • 14
  • 2
    This would be considered by some as off-topic. You only ask for code on how to do some particular task, and show no attempts or research effort. On this point, read the information provided in the [help center](http://stackoverflow.com/help/asking). Regarding Stata, try `help string functions`. – Roberto Ferrer May 18 '15 at 13:21
  • I have tried, i have done all the itrim, rtrim functions to get the postcodes into this format XXXX XXX but some have no spaces in them and some do. For this reason, i removed all spaces and not want to add in a space three characters from the end but am unsure how to. Some postcodes have three characters at the beginning and some have three so cannot specify a specific cut off point to split the variable – Exodia16 May 19 '15 at 09:19

1 Answers1

1

The code given

gen str8=postcodenew=subinstr(postcode," ", "", .)

is illegal but presumably a typo for

gen str8 postcodenew = subinstr(postcode," ", "", .)

What is wanted may be

gen postcodenew2 = substr(postcodenew, 1, length(postcodenew) - 3) + " " + substr(postcodenew, -3, 3) 
Nick Cox
  • 35,529
  • 6
  • 31
  • 47