Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st:how to delete anything in the bracket for a string variable


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st:how to delete anything in the bracket for a string variable
Date   Sun, 9 Oct 2011 08:27:10 +0100

Tirthankar's kind of solution is much to be preferred, but there is no
reason to suppose that -substr()- does not work here

In Amanda's examples, this should work:

replace name = substr(name, 1, strpos(name, "(") - 1)

This is not in one-to-one correspondence with Tirthankar's solution,
as it assumes that the important stuff comes first, and it also zaps
anything after the ")", but the solution is then just more
complicated, not impossible.

Many people call () parentheses, sometimes round brackets; [] brackets
or square brackets, and {} braces or curly brackets.

Either way, -trim()- is also useful here to remove any leading or
trailing spaces.

Nick

On Sun, Oct 9, 2011 at 6:05 AM, Tirthankar Chakravarty
<tirthankar.chakravarty@gmail.com> wrote:

> This should work:
>
> ***********************************************
> clear*
> input id   str50     name
> 1         "John (joined before 2005)"
> 2         "Jenny"
> 3         "Kai (joined in 2006; left in 2008)"
> 4         "Tom (inconsistent dates)"
> end
> replace name = regexr(name, "\((.)+\)", "")
> li
> ***********************************************
>
> T
>
> On Sat, Oct 8, 2011 at 9:17 PM, Amanda Fu <mandy.fu1@gmail.com> wrote:

>> I wonder if someone can give me some suggestion on delete anything in
>> the bracket for a string variable. A string variable in my data set
>> has some notes in bracket for part of the observations. I want to
>> remove the brackets and anything in them
>>
>> An example is like:
>> -------------------------
>> id        name
>> 1         John (joined before 2005)
>> 2         Jenny
>> 3         Kai (joined in 2006; left in 2008)
>> 4         Tom (inconsistent dates)
>> -------------------------.......
>> I would like to delete anything in the () for the variable "name" so
>> that only names are kept. But the content in the brackets are not the
>> same. I think that causes the -substr- does not work for this purpose.

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index