Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: replace and strpos


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: replace and strpos
Date   Wed, 15 Aug 2012 12:00:43 +0100

Daniel had presumably not seen my post when writing this, but the
answer to his question is that "W1" and "NW1" are only the starts of
the postcodes.

Nick

On Wed, Aug 15, 2012 at 11:47 AM, Daniel Feenberg <feenberg@nber.org> wrote:
>
>
> On Wed, 15 Aug 2012, Ona McCarthy 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.
>>
>
> I am interested in why the character comparison operator isn't suitable:
>
>    replace borough = "Westinster" if postcode == "W1"
>
> Alternatively, one could create a file of postcodes and boroughs, and merge
> it with the in-memory dataset by borough.
*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index