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

# st: RE: AW: Dropping string observations that have a sequence of characters

 From "Nick Cox" To Subject st: RE: AW: Dropping string observations that have a sequence of characters Date Wed, 28 Apr 2010 11:04:33 +0100

```I'd agree with Martin that -strpos()- offers the simplest solution here.
There is no need for, although no harm in, regexp solutions.

But George's example, although presumably concocted rather than
indicative of an astronomical or astrological problem, raises a detail
that could be important for his real problem. Suppose you capitalised
the planet names consistently. Then

if strpos(myvar, "ur") > 0

would not catch "Uranus". If you wanted that to happen, there are
various solutions including

if strpos(lower(myvar), "ur") > 0

The abbreviation

if strpos(lower(myvar), "ur")

is also possible here, as in this kind of problem the result of
-strpos()- is non-zero (regarded as logically true) precisely when it is
positive. That is -strpos()- cannot return a negative value.

Nick
n.j.cox@durham.ac.uk

Martin Weiss

*************
clear*

input str7 myvar
"venus"
"mercury"
"Jupiter"
"mars"
"saturn"
"uranus"
"Neptune"
end

list, noo

drop if strpos(myvar , "ur")!=0

list, noo
*************
George Chioran

How can you delete observations from a variable that contains strings
that have
the letters "ur" for instance.

For example if the variable has the observations: "venus" "mercury"
"mars"
"Jupiter" "saturn" "uranus" "Neptune". The how could I delete the
observations that contain the letters "ur", in this case "saturn" and
"uranus" ?

*
*   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/
```