Stata destring with removing weird characters

STATA destring with only keeping numeric numbers

The newest solution to this problem is:
egen-method as described below followed by:

replace problemvar = subinstr(problemvar, ",", ".",.)
https://www.stata.com/statalist/archive/2009-05/msg00862.html

followed by:
destring problemvar, replace *without the 'dpcomma' option.


Dieter Van Der Westhuizen
Mon 2020-07-27 03:01 PM
Sent Items
To:Jody Rusch (jody.rusch@nhls.ac.za);
Follow-up from my previous email:

*This command works when one has either comma or period in the field:

destring kknew3, replace dpcomma 
Dieter Van Der Westhuizen
Mon 2020-07-27 02:39 PM
Sent Items
How to strip special characters in STATA variables:
Type the following:
***********************************
program define extrnum
version 7
syntax varlist(max=1) , gen(str)
local maxlen: type `varlist'
local maxlen=substr("`maxlen'",4,.)
tempvar work
qui gen str1 `work'=""
forvalues i=1/`maxlen' {
qui replace `work'=`work'+substr(`varlist',`i',1) if real(substr(`varlist',`i',1))<.
}
gen `gen'=real(`work')
end
*************************************
How to use:
extrnum var1, gen(newvar1)
 
It works perfectly except it drops the commas too:
**Thus use:
findit egenmore
**and install the app “egenmore” by following the instructions. 
**Then one can specify to keep the following characters:
egen kknew3 = sieve(kk), char(0123456789.,) 
**Which lets you end up with this:

Destringing this doesn’t work yet, as some characters have “.” and others have “,”

Dieter Van Der Westhuizen
Mon 2020-07-27 11:30 AM
Sent Items

https://www.statalist.org/forums/forum/general-stata-discussion/general/967675-removing-non-numeric-characters-from-strings

Dieter van der Westhuizen
Chemical Pathology RegistrarC17 NHLS Pathology Laboratory, Groote Schuur Hospital and Red Cross Children’s Hospital LaboratoryTel: 021 404 4135 | Cell: +27 82 861 2093 | Fax-to-email: 0866 090 397dieter.vdwesthuizen@nhls.ac.za | www.nhls.ac.za