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: cleaning string variables


From   Steven Samuels <sjsamuels@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: cleaning string variables
Date   Mon, 11 Jul 2011 18:22:03 -0400

Elizabeth-

You are missing some elementary information about Stata syntax and regular expressions.  Your second statement did not include a "replace" command, but I assume this is just a typo, because it did not trigger an error message. What did trigger the message is that you did not replace "newvar" _with_ anything. Moreover  "*" is not a regular expression wildcard, so if you hadn't committed the first error, the regular expression  would  still have failed. (And a wild card was completely unnecessary, as you can see below.) Finally, if you had correctly typed the regular expression
***********************************************
replace newvar = 1 if regexm(oldvar,"/ABC/ABC")
***********************************************
you would have received a "type mismatch" error, because newvar was set equal to a string variable (oldvar) and could not be replaced with a numeric value.

Finally, in this example, a regular expression was unnecessary. 

*****************
clear
input str20 oldvar
"ABC"
"DEF/ABC"
"DEF"
"DEF/ABC/ABC"
end
list
gen z = 0
replace z=1 if regexm(oldvar, "/ABC/ABC")
gen w = 0
replace w = 1 if strpos(oldvar,"/ABC/ABC")>0
list
***************


Steve
sjsamuels@gmail.com



On Jul 11, 2011, at 5:42 PM, Elizabeth Sanders wrote:

I have a string variable of the form

ABC
DEF/ABC
DEF
DEF/ABC/ABC

etc.,

and want to create a new variable that is 1 if the value includes at least /ABC/ABC

I expect I will be searching for */ABC/ABC*, so I tried

gen newvar=oldvar
newvar if regexm(newvar, "*/ABC/ABC*")

but got an error "=exp required".

What am I missing?

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


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