Scenario: You get a dataset that has currency values listed in the form ($1,045.65), instead of the preferred (1045.65). If you’re using Stata, making the transformation is easy. Say your variable is named price. Then

destring price , ignore(“,, $”) replace

will make the conversion.

The code above simply takes advantage of the powerful destring statement. One of its options is ignore(“chars”), which tells it to ignore certain characters when converting from string to numeric. An example of a destring statement using the ignore() option is

destring x , ignore (“%, #”)

This would strip all of the %’s and #’s from the variable x and turn it into a numeric variable.

However, in the price example above, one of the characters we want to ignore is the comma — but the comma is also used to delimit the list of characters that the ignore option takes as arguments. So we use the somewhat awkward statement

…ignore(“,, $”)

to eliminate both commas and dollar signs.


  • Quick navigation

  • Categories