Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: replace and strpos |
Date | Wed, 15 Aug 2012 10:29:37 +0100 |
As this is an international list, an appropriate reference is http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom (Usually, parochialism bites in reverse by members assuming that what is clear in California or true in Texas must be so elsewhere.) You can reduce your problems considerably by insisting that ... if strpos(postcode, "NW1") == 1 ... if strpos(postcode, "W1") == 1 which is going to be true for these cases. Make sure that you -trim()- strings of leading spaces. You can avoid overwriting by insisting that only empty values are -replace-d. replace borough = "Westminster" if borough == "" & strpos(postcode, "W1") == 1 Nick On Wed, Aug 15, 2012 at 10:12 AM, Ona McCarthy <omccarthy@tccr.org.uk> wrote: > I have a list of around 300 postcodes that are string variables. I need to change the postcode strings into their corresponding borough. I succeeded in doing this to some extent by writing commands for each string value using replace and strops. E.g.: > > replace borough = "Camden" if strpos(postcode, "NW1") > > A problem arises when I run all the commands and it comes to a postcode that contains some characters that are also in another postcode, i.e. it overwrites the original. > > E.g. the above "Camden" is overwritten by "Westminster" when this command is run: > > replace borough = "Westminster" if strpos(postcode, "W1") > > Because it shares the "W1". > > Is there any way I can tell Stata to look for the EXACT postcode? I've looked all around and can't figure it out. * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/