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]

st: Remove prefixes (e.g., >, <, and +/-) from numbers stored as strings


From   Richard Herron <richard.c.herron@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: Remove prefixes (e.g., >, <, and +/-) from numbers stored as strings
Date   Fri, 8 Jun 2012 13:29:48 -0400

I have numbers stored as string with prefixes (e.g., "+/-30") that I
would like to convert to numbers. Not all entries necessarily have
prefixes (or postfixes).

With -regexm()- and -regexs()- I can remove from postfixes and handle
decimals, but I can't remove prefixes. Can you spot my error with
-regexm()-? Thanks!

Richard Herron

* begin code
clear
set obs 20
generate number = 100*runiform()
generate prefix = ""
generate postfix = ""
foreach i of numlist 1 5 10 15 {
    replace prefix = ">" in `i'
    replace postfix = "%" in `=`i' + 1'
    replace number = int(number) in `=`i' + 2'
}
egen combo = concat(prefix number postfix)
generate number2 = regexs(1) if regexm(combo, "([0-9]*\.?[0-9]*)")
list
* end code
*
*   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