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]

st: String function headache.


From   Scott Talkington <[email protected]>
To   [email protected]
Subject   st: String function headache.
Date   Sun, 24 Apr 2011 23:45:28 -0400

I just can't seem to make this work. What I want to do is search for any occurrence of the "#" character in a string variable and set a flag for that observation. I'm searching 6 different strings labeled something like mystring1 mystring2 etc. and the flags are mynumber1 mynumber2 etc..

So my do file:

forvalues x=1/6 {
foreach y in # {
replace mynumber `x'= strmatch(mistring`x', "`y'")
}
}

I just listed one character in the y list above, but in reality I'm not having a problem with normal strings like "APT" but with wildcards and with the number sign character itself.

I assumed that placing a "?" character iyn the search string (s2) would match zero or one characters + the "#" but it seems to be matching all strings with one character that are either a number or a letter. Huh?

If I include the wildcard (either the asterisk or the question mark) *anywhere* (either in the "foreach" part of the do file or in the "replace" command) it just doesn't work the way I expect it to. There's a difference between what I get depending on how many quotes I use and where as well, but I'm just not getting anything that does what I want it to. I've even tried using the backslash character to indicate that I don't want the "#" to be read as an operator, but I'm not even sure where to put the backslash or how to arrange the quotation marks. It's driving me nuts. There's some rule here that I'm just not getting.

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