Ddestring
Ddestring
Description
destring converts variables in varlist from string to numeric. If varlist is not specified, destring
will attempt to convert all variables in the dataset from string to numeric. Characters listed in ignore()
are removed. Variables in varlist that are already numeric will not be changed. destring treats both
empty strings “ ” and “.” as indicating sysmiss (.) and interprets the strings “.a”, “.b”, . . . , “.z” as
the extended missing values .a, .b, . . . , .z; see [U] 12.2.1 Missing values. destring also ignores any
leading or trailing spaces so that, for example, “ ” is equivalent to “ ” and “ . ” is equivalent to “.”.
tostring converts variables in varlist from numeric to string. The most compact string format pos-
sible is used. Variables in varlist that are already string will not be converted.
Quick start
Convert strg1 from string to numeric, and place result in num1
destring strg1, generate(num1)
Same as above, but ignore the % character in strg1
destring strg1, generate(num1) ignore(%)
Same as above, but return . for observations with nonnumeric characters
destring strg1, generate(num1) force
Convert num2 from numeric to string, and place result in strg2
tostring num2, generate(strg2)
Same as above, but format with a leading zero and 3 digits after the decimal
tostring num2, generate(strg2) format(%09.3f)
Menu
destring
Data > Create or change data > Other variable-transformation commands > Convert variables from string to
numeric
tostring
Data > Create or change data > Other variable-transformation commands > Convert variables from numeric to
string
1
destring — Convert string variables to numeric variables and vice versa 2
Syntax
Convert string variables to numeric variables
destring [ varlist ], { generate(newvarlist ) | replace } [ destring options ]
destring
Example 1
We read in a dataset, but somehow all the variables were created as strings. The variables contain no
nonnumeric characters, and we want to convert them all from string to numeric data types.
. use https://www.stata-press.com/data/r19/destring1
. describe
Contains data from https://www.stata-press.com/data/r19/destring1.dta
Observations: 10
Variables: 5 3 Mar 2024 10:15
id str3 %9s
num str3 %9s
code str4 %9s
total str5 %9s
income str5 %9s
Sorted by:
. list
. destring, replace
id: all characters numeric; replaced as int
num: all characters numeric; replaced as int
code: all characters numeric; replaced as int
total: all characters numeric; replaced as long
income: all characters numeric; replaced as long
destring — Convert string variables to numeric variables and vice versa 5
. describe
Contains data from https://www.stata-press.com/data/r19/destring1.dta
Observations: 10
Variables: 5 3 Mar 2024 10:15
id int %10.0g
num int %10.0g
code int %10.0g
total long %10.0g
income long %10.0g
Sorted by:
Note: Dataset has changed since last saved.
. list
Example 2
Our dataset contains the variables date, price, and percent. These variables were accidentally read
into Stata as string variables because they contain spaces, dollar signs, commas, and percent signs. We
will leave the date variable as a string so that we can use the date() function to convert it to a numeric
date. For price and percent, we want to remove all of the nonnumeric characters and create new
variables containing numeric values. After removing the percent sign, we want to convert the percent
variable to decimal form.
destring — Convert string variables to numeric variables and vice versa 6
Sorted by:
. list
Sorted by:
Note: Dataset has changed since last saved.
destring — Convert string variables to numeric variables and vice versa 7
. list
tostring
Conversion of numeric data to string equivalents can be problematic. Stata, like most software, holds
numeric data to finite precision and in binary form. See the discussion in [U] 13.12 Precision and
problems therein. If no format() is specified, tostring uses the format %12.0g. This format is, in
particular, sufficient to convert integers held as bytes, ints, or longs to string equivalent without loss of
precision.
However, users will often need to specify a format themselves, especially when the numeric data have
fractional parts and for some reason a conversion to string is required.
Example 3
Our dataset contains a string month variable and numeric year and day variables. We want to convert
the three variables to a %td date.
. use https://www.stata-press.com/data/r19/tostring, clear
. list
Saved characteristics
Each time the destring or tostring commands are issued, an entry is made in the characteristics
list of each converted variable. You can type char list to view these characteristics.
After example 2, we could use char list to find out what characters were removed by the destring
command.
. char list
price2[destring]: Characters removed were: $ ,
price2[destring_cmd]: destring price percent, generate(price2 percent..
percent2[destring]: Character removed was: %
percent2[destring_cmd]: destring price percent, generate(price2 percent..
Video example
How to convert a string variable to a numeric variable
Acknowledgment
destring and tostring were originally written by Nicholas J. Cox of the Department of Geography
at Durham University, UK, who is coeditor of the Stata Journal and author of Speaking Stata Graphics.
References
Cox, N. J. 1999a. dm45.1: Changing string variables to numeric: Update. Stata Technical Bulletin 49: 2. Reprinted in
Stata Technical Bulletin Reprints, vol. 9, p. 14. College Station, TX: Stata Press.
———. 1999b. dm45.2: Changing string variables to numeric: Correction. Stata Technical Bulletin 52: 2. Reprinted in
Stata Technical Bulletin Reprints, vol. 9, p. 14. College Station, TX: Stata Press.
———. 2011. Speaking Stata: MMXI and all that: Handling Roman numerals within Stata. Stata Journal 11: 126–142.
Cox, N. J., and W. W. Gould. 1997. dm45: Changing string variables to numeric. Stata Technical Bulletin 37: 4–6.
Reprinted in Stata Technical Bulletin Reprints, vol. 7, pp. 34–37. College Station, TX: Stata Press.
Cox, N. J., and C. B. Schechter. 2018. Speaking Stata: Seven steps for vexatious string variables. Stata Journal 18:
981–994.
destring — Convert string variables to numeric variables and vice versa 9
Cox, N. J., and J. B. Wernow. 2000a. dm80: Changing numeric variables to string. Stata Technical Bulletin 56: 8–12.
Reprinted in Stata Technical Bulletin Reprints, vol. 10, pp. 24–28. College Station, TX: Stata Press.
———. 2000b. dm80.1: Update to changing numeric variables to string. Stata Technical Bulletin 57: 2. Reprinted in
Stata Technical Bulletin Reprints, vol. 10, pp. 28–29. College Station, TX: Stata Press.
Jeanty, P. W. 2013. Dealing with identifier variables in data management and analysis. Stata Journal 13: 699–718.
Also see
[D] egen — Extensions to generate
[D] encode — Encode string into numeric and vice versa
[D] generate — Create or change contents of variable
[D] split — Split string variables into parts
[FN] String functions
Stata, Stata Press, and Mata are registered trademarks of StataCorp LLC. Stata and Stata
®
Press are registered trademarks with the World Intellectual Property Organization of the
United Nations. StataNow and NetCourseNow are trademarks of StataCorp LLC. Other
brand and product names are registered trademarks or trademarks of their respective com-
panies. Copyright © 1985–2025 StataCorp LLC, College Station, TX, USA. All rights
reserved.
For suggested citations, see the FAQ on citing Stata documentation.