Combining strings into one variable

I’m again playing around with strings in Stata and need to combine (string) variables with (string) variables as well as strings in locals with string variables. Suppose you have two string variables (strvar1 and strvar2) and want to combine them, you can simply type gen combinedvar=strvar1 + strvar2 Or, if you would like to have…

I’m again playing around with strings in Stata and need to combine (string) variables with (string) variables as well as strings in locals with string variables. Suppose you have two string variables (strvar1 and strvar2) and want to combine them, you can simply type

gen combinedvar=strvar1 + strvar2

Or, if you would like to have the words in strvar1 and strvar2 separated by a blank, type

gen combinedvar=strvar1 + " " + strvar2

Alternatively you could also use the egen concat function (egen combinedvar=concat(strvar1 strvar2)) with added “punct(" ")” to separate the strings from each other.

Leave a Reply