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
"Seed, Paul" <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: replace and strpos
Date
Thu, 16 Aug 2012 10:23:39 +0100
Ona might do better to use a different command:
UK postcodes are made of two parts separated by a
space. The first part identifies the postcode district
(not the borough); and indeed (except when an A is added)
is the correct name of the postcode district.
The word function will do what is needed.
replace borough = "Camden" if word(postcode, 1) == "NW1"
replace borough = "Camden" if word(postcode, 1) == "NW1A"
replace borough = Westminster" if word(postcode, 1) == "W1"
To get the correct postcode districts, it is quicker to use:
gen postcode_district = word(postcode, 1)
replace postcode_district = substr(postcode_district, 1, length(postcode_district)-1) if substr(postcode_district, length(postcode_district),1) == "A"
However, the London Borough of Camden in fact occupies parts of NW1, NW2, NW3 (all),
NW5, NW6 and NW8; and shares NW1 with Westminster.
There are publicly available files that contain this information
If Ona really wants the Boroughs and other local authorities, she should merge her file with
one of the publicly available data bases. (Free to academics, commercial organisations may have to pay)
Some useful addresses:
http://borders.edina.ac.uk/html/pcluts_main.html
http://en.wikipedia.org/wiki/NW_postcode_area
http://www.ons.gov.uk/ons/guide-method/geography/products/postcode-directories/-nspp-/onspd-user-guide-and-version-notes.zip
http://www.ons.gov.uk/ons/guide-method/geography/products/postcode-directories/-nspp-/index.html
Paul T Seed, Senior Lecturer in Medical Statistics,
Division of Women's Health, King's College London
Women's Health Academic Centre, KHP
(+44) (0) 20 7188 3642.
On Wed, Aug 15, 2012 at 10:12 AM, Ona McCarthy <[email protected]> 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/
On Wed, Aug 15, 2012 at 10:12 AM, Ona McCarthy <[email protected]> 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/
Paul T Seed, Senior Lecturer in Medical Statistics,
Division of Women's Health, King's College London
Women's Health Academic Centre, KHP
(+44) (0) 20 7188 3642.
*
* 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/