Example: barber

destring — Convert string variables to numeric variables ...

Convert string variables to numeric variables and vice versaSyntaxMenuDescriptionOptions for destringOptions for tostringRemarks and examplesAcknowledgmentReferencesAlso seeSyntaxConvert string variables to numeric variablesdestring[varlist],{generate(new varlist)|replace}[destringoptions]Conver t numeric variables to string variablestostringvarlist,{generate(newva rlist)|replace}[tostringoptions] destring optionsDescription generate(newvarlist)generatenewvar1,..,n ewvarkfor each variable invarlist replacereplace string variables invarlistwith numeric variablesignore("chars")remove specified nonnumeric charactersforceconvert nonnumeric strings to missing valuesfloatgenerate numeric variables as typefloatpercentconvert percent variables to fractional formdpcommaconvert variables with commas as decimals to period-decimal format Eithergenerate(newvarlist)orreplaceis generate(new)

destring— Convert string variables to numeric variables and vice versa 5 Example 2 Our dataset contains the variable date, which was accidentally recorded as a string because of spaces after the year and month. We want to remove the spaces. destring will convert it to numeric and remove the spaces.

Tags:

  Variable, Numeric, String, Convert, Destring, Destring convert string variables to numeric variables

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of destring — Convert string variables to numeric variables ...

1 Convert string variables to numeric variables and vice versaSyntaxMenuDescriptionOptions for destringOptions for tostringRemarks and examplesAcknowledgmentReferencesAlso seeSyntaxConvert string variables to numeric variablesdestring[varlist],{generate(new varlist)|replace}[destringoptions]Conver t numeric variables to string variablestostringvarlist,{generate(newva rlist)|replace}[tostringoptions] destring optionsDescription generate(newvarlist)generatenewvar1,..,n ewvarkfor each variable invarlist replacereplace string variables invarlistwith numeric variablesignore("chars")remove specified nonnumeric charactersforceconvert nonnumeric strings to missing valuesfloatgenerate numeric variables as typefloatpercentconvert percent variables to fractional formdpcommaconvert variables with commas as decimals to period-decimal format Eithergenerate(newvarlist)orreplaceis generate(newvarlist)generatenewvar1.

2 ,newvarkfor each variable invarlist replacereplace numeric variables invarlistwith string variablesforceforce conversion ignoring information lossformat(format) Convert using specified formatusedisplayformatconvert using display format Eithergenerate(newvarlist)orreplaceis >Create or change data>Other variable -transformation commands> Convert variables from string tonumerictostringData>Create or change data>Other variable -transformation commands> Convert variables from numeric tostring12 destring Convert string variables to numeric variables and vice versaDescriptiondestringconverts variables invarlistfrom string to numeric .

3 Ifvarlistis not specified,destringwill attempt to Convert all variables in the dataset from string to numeric . Characters listed inignore()are removed. variables invarlistthat are already numeric will not be bothempty strings and . as indicating sysmiss (.) and interprets the strings .a , .b ,.., .z asthe extended missing ,.b,..,.z; see[U] Missing ignoresany leading or trailing spaces so that, for example, is equivalent to and . is equivalent to ..tostringconverts variables invarlistfrom numeric to string . The most compact string formatpossible is used.

4 variables invarlistthat are already string will not be for destringEithergenerate()orreplacemust be specified. With either option, if any string variablecontains nonnumeric characters not specified withignore(), then no corresponding variable will begenerated, nor will that variable be replaced (unlessforceis specified).generate(newvarlist)specifies that a new variable be created for each variable contain the same number of new variable names as there are variables invarlist. Ifvarlistisnot specified,destringattempts to generate a numeric variable for each variable in the dataset;newvarlistmust then contain the same number of new variable names as there are variables in thedataset.

5 Any variable labels or characteristics will be copied to the new variables that the variables invarlistbe converted to numeric variables . Ifvarlistis notspecified,destringattempts to Convert all variables from string to numeric . Any variable labelsor characteristics will be ("chars")specifies nonnumeric characters to be removed. If any string variable contains anynonnumeric characters other than those specified withignore(), no action will take place for thatvariable unlessforceis also specified. Note that to Stata the comma is a nonnumeric character;see also thedpcommaoption that any string values containing nonnumeric characters, in addition to any specifiedwithignore(), be treated as indicating missing numeric that any new numeric variables be created initially as typefloat.

6 The default is typedouble; see [D]data automatically to compress each new numericvariable after any percent signs found in the values of a variable , and all values of that variableare divided by 100 to Convert the values to fractional itself implies that thepercent sign, % , is an argument toignore(), but the converse is not that variables with commas as decimal values should be converted to have periodsas decimal for tostringEithergenerate()orreplacemust be specified. If converting any numeric variable to stringwould result in loss of information, no variable will be produced unlessforceis specified.

7 For moredetails, Convert string variables to numeric variables and vice versa 3generate(newvarlist)specifies that a new variable be created for each variable contain the same number of new variable names as there are variables invarlist. Any variablelabels or characteristics will be copied to the new variables that the variables invarlistbe converted to string variables . Any variable labelsor characteristics will be that conversions be forced even if they entail loss of information. Loss of informationmeans one of two circumstances: 1) The result ofreal( string (varname, "format"))is notequal tovarname; that is, the conversion is not reversible without loss of information; 2)replacewas specified, but a variable has associated value labels.

8 In circumstance 1, it is usually best tospecifyusedisplayformatorformat(). In circumstance 2, value labels will be ignored in aforced (see [D]encode) is the standard way to generate a string variable basedon value (format)specifies that a numeric format be used as an argument to thestring()function,which controls the conversion of the numeric variable to string . For example, a format of% that numbers are to be rounded to two decimal places before conversion to string . SeeRemarks and examplesbelow and [D]functionsand [D] ()cannot be that the current display format be used for each variable .

9 For example,this option could be useful when using Social Security numbers or daily or other dates withsome%dor%tformat be specified withformat().Remarks and are presented under the following headings:destringtostringdestringExample 1We read in a dataset, but somehow all the variables were created as strings. The variables containno nonnumeric characters, and we want to Convert them all from string to numeric data use describeContains data from : 10vars: 5 3 Mar 2013 10:15size: 200storage display valuevariable name type format label variable labelid str3 %9snum str3 %9scode str4 %9stotal str5 %9sincome str5 %9sSorted by:4 destring Convert string variables to numeric variables and vice versa.

10 Listid num code total 243 1234 543 123 2345 67854 234 3456 345 345 4567 57 456 5678 23 567 6789 23465 678 7890 65 789 8976 23 901 7654 23 890 6543 423 19234. destring , replaceid has all characters numeric ; replaced as intnum has all characters numeric ; replaced as intcode has all characters numeric ; replaced as inttotal has all characters numeric ; replaced as longincome has all characters numeric ; replaced as long. describeContains data from : 10vars: 5 3 Mar 2013 10:15size: 140storage display valuevariable name type format label variable labelid int % int % int % long % long % by:Note: dataset has changed since last saved.


Related search queries