Strings to unique integers in Stata

Suppose you have a string variable with a finite number of values (e.g. US states, industry, etc). The encode command plus the generate command will create a new variable that assigns unique ids to each string value:

encode your_string_variable, gen(new_string_as_int)

The labels in the edit/browse view will look like strings, so make sure you use ‘nolabel’ on any export or tab commands.

Leave a Reply