This page shows examples of how one might use string related commands in STATA. The first column shows the code you would use, the second column shows how your data might look like before applying the code, and the third column shows how your data would look like after applying the code.

Besides applying the commands below to data, you also may want to apply the same commands to STATA macros (locals and globals). I will be preparing a post that tackles such problems as well.





replace var = lower(var)
replace var = upper(var)

“some text”

“some text”

Changing the case from upper to lower and vice-versa.

replace var = subinstr(var,”Jr .”,””,.)
replace var = subinstr(var,”Greece”,”GR”,.)

“John Jr. Smith”

“John Smith”

Remove the “Jr. ” part from the string; Shorten country name.

replace var = substr(var,1,10)
replace var = substr(var,1,strpos(var,” “))

“20/02/2020 12:35”
“20/02/2020 12:35”


Extract the desired part of a string (using two different methods)

replace var = substr(var,-5,5)

“20/02/2020 12:35”


Extract the last 5 characters from a string

replace var = strtoname(var)

” 1v 123″


Convert a string to a format that can be later used as variable name

replace var = strtrim(var)
replace var = strltrim(var)
replace var = strrtrim(var)

” extra spaces “
” extra spaces “
” extra spaces “

“extra spaces”
“extra spaces “
” extra spaces”

Remove leading spaces, trailing spaces, or both.

split var, parse(“/”)


“29” “01” “1987”

Split one variable into multiple ones based on a specific parsing character

gen newvar = strpos(var,”New”)

“10250 New York”


Find out the location of a substring in a string

gen newvar = reverse(var)

“New York”

“kroY weN”

Reverse the content of a string

tostring var, g(newvar)
gen newvar = string(var)



Convert numeric variable to string, using two different methods