,

Remove blanks from string variables in Stata

Identify and remove blank space in Stata’s string variables

When dealing with string variables in Stata, blanks spaces can make it difficult to identify values. For example, if a variable contains " Arizona", a command that contains an if command such as ... if state="Arizona" won’t detect this observation.

To trim blank spaces (ASCII space character char(32)) at the beginning or the end of the value, Stata has different built-in functions:

* Removes blanks at beginning and end
replace state=trim(state)
* Removes blanks at beginning only
replace state=ltrim(state)
* Removes blanks at end only
replace state=rtrim(state)
* Removes all blanks in string (irrespective of place)
replace state=subinstr(state," ","",.)

To also remove other blank spaces (eg horizontal tabs), please use the ustr functions which also remove ASCII codes char(9), char(10), char(11), char(12), and char(13):

* Removes blanks at beginning and end
replace state=ustrtrim(state)
* Removes blanks at beginning only
replace state=ustrltrim(state)
* Removes blanks at end only
replace state=ustrrtrim(state)

Response to “Remove blanks from string variables in Stata”

  1. Useful string functions in Stata | StataTex Blog

    […] Uncleaned data often contains unwanted empty spaces (such as state=" Arizona"). To remove those, you can use Stata’s trim functions (see also here): […]

Leave a Reply