Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

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

From |
Kamarul Imran <kim4597@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: replace part of observations in a string variable |

Date |
Wed, 17 Oct 2012 11:34:55 +0100 |

Dear Nick, Your solution works like charm !!! Thanks very much.And thanks to Muhammad too. Regards. Kamarul On Wed, Oct 17, 2012 at 11:21 AM, Nick Cox <njcoxstata@gmail.com> wrote: > It's nice of Muhammad to quote a posting of mine, but he doesn't spell > out how it answers the question here. > > First off, Kamarul writes of a variable -new2- but applies code to a > variable -addnew2-, so that confusion may be the answer. > > Second, Kamarul is testing for _exact_ equality and if that fails one > possibility is that there are leading and trailing spaces so that > > replace addnew2=" " if trim(addnew2) == "Pasir Mas" > > or > > replace addnew2=" " if strpos(addnew2, "Pasir Mas") > > would be work-arounds in that case. > > Either way, a broad strategic answer is "find out about string > functions", which was I suspect Muhammad's implicit point. > > Nick > > On Wed, Oct 17, 2012 at 11:08 AM, Muhammad Anees <anees@aneconomist.com> wrote: > >> This can be for you. >> >> http://www.stata.com/statalist/archive/2011-06/msg00393.html > > On Wed, Oct 17, 2012 at 3:02 PM, Kamarul Imran <kim4597@gmail.com> wrote: > >>> I want to replace part of observations in the string variable to >>> missing values. For example, I want to replace "Pasir Mas" to either >>> "." or " " from a string variable named as "new2" >>> >>> ------------------------------------ >>> new2 >>> ------------------------------------ >>> Tumpat >>> Tumpat >>> Kota Bharu >>> Kota Bharu >>> Pasir Mas >>> Kota Bharu >>> Kota Bharu >>> -------------------------------------- >>> >>> what I did was: >>> >>> . replace addnew2=" " if addnew2 == "Pasir Mas" >>> (0 real changes made) >>> >>> Observation "Pasir Mas" was not replaced by " " . What have I done >>> wrong? What is the better way of working with string variable? > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**References**:**st: replace part of observations in a string variable***From:*Kamarul Imran <kim4597@gmail.com>

**Re: st: replace part of observations in a string variable***From:*Muhammad Anees <anees@aneconomist.com>

**Re: st: replace part of observations in a string variable***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**Re: st: replace part of observations in a string variable** - Next by Date:
**Re: st: Assign observations to the right week number** - Previous by thread:
**Re: st: replace part of observations in a string variable** - Next by thread:
**Re: st: replace part of observations in a string variable** - Index(es):