UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Code Fragment
Replacing All Character Values in a String Variable with a Single Numeric or Character Value

This is a code fragment that defines a program fixnumeric to replace all character values (i.e., a, b, D, etc.) embedded in a string variable with either a single numeric or character value. The options for this program are var( ) which defines the problematic string variable, gen( ) defines the name of the new variable with corrections and replace( ) where all character values in the variable given in var( ) is replaced by the value given in replace( ). We provide an example to illustrate this program. Note the program assumes that the variable in var( ) has no missing values and is made up of one word.

capture program drop fixnumeric
program define fixnumeric
syntax, var(varname string) gen(string) replace(string)  

set more off
quietly {
local typevar : type `var'
gen `typevar' `gen' = ""
count
local N = r(N)
forvalues i = 1/`N' {
local ii = `var'[`i']
local lii = strlen("`ii'") 
  forvalues j = 1/`lii'{
  local js = substr("`ii'",`j',1)
    local c0 = "`js'" == "0"
    local c1 = "`js'" == "1"
    local c2 = "`js'" == "2"
    local c3 = "`js'" == "3"
    local c4 = "`js'" == "4"
    local c5 = "`js'" == "5"
    local c6 = "`js'" == "6"
    local c7 = "`js'" == "7"
    local c8 = "`js'" == "8"
    local c9 = "`js'" == "9"
  local sumc = `c0'+`c1'+`c2'+`c3'+`c4'+`c5'+`c6'+`c7'+`c8'+`c9'
  if `sumc' != 1 {
    local ii2 = subinstr("`ii'","`js'","`replace'",1)
    local ii = "`ii2'"
                 } 
  if `sumc' == 1 {
    local ii2 = "`ii'"
                 } 
}
replace `gen' = "`ii2'" if _n == `i'
}
}
end

clear
input str6 x1
1b123
12a1xc
12345a
xazaa1
12345
end

fixnumeric, var(x1) gen(x1_fix1) replace(0)
fixnumeric, var(x1) gen(x1_fix2) replace(X)
list

     +----------------------------+
     |     x1   x1_fix1   x1_fix2 |
     |----------------------------|
  1. |  1b123     10123     1X123 |
  2. | 12a1xc    120100    12X1XX |
  3. | 12345a    123450    12345X |
  4. | xazaa1    000001    XXXXX1 |
  5. |  12345     12345     12345 |
     +----------------------------+

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California