Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create string variable referencing other string variables in Stata?

Tags:

stata

I currently have 2 variables, state and year, into which I wish to convert into 1 variable, stateyear.

I want the stateyear variable to have values in the following form: state_year (e.g. Texas_1962).

How can I reference the values in the state and year variables to create the new stateyear variable?

like image 716
Jess Avatar asked Jan 17 '26 21:01

Jess


1 Answers

That could be

 gen state_year = state + "_" + string(year) 

where I am assuming that year is numeric. Or it could be

 egen state_year = concat(state year), p(_) 

which takes care of any type conversions needed.

Or it could be

 egen state_year = group(state year), label 

which doesn't give you a connecting underscore. That raises a key point: why you do think you need that underscore? It will just look ugly on graphs or tables. If spaces are (thought to be) a problem, what about "North Carolina_2013", and so forth?

For a miniature review of this question, see http://www.stata-journal.com/sjpdf.html?articlenum=dm0034

like image 192
Nick Cox Avatar answered Jan 19 '26 19:01

Nick Cox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!