Reputation: 61
I am using Stata 14. I have US states and corresponding regions as integer. I want create a string variable that represents the region for each observation. Currently my code is
gen div_name = "A"
replace div_name = "New England" if div_no == 1
replace div_name = "Middle Atlantic" if div_no == 2
.
.
replace div_name = "Pacific" if div_no == 9
..so it is a really long code.
I was wondering if there is a shorter way to do this where I can automate assigning values rather than manually hard coding them.
Upvotes: 0
Views: 283
Reputation: 37208
You can define value labels in one line with label define
and then use decode
to create the string variable. See the help
for those commands.
If the correspondence was defined in a separate dataset you could use merge
. See e.g. this FAQ
There can't be a short-cut here other than typing all the names at some point or exploiting the fact that someone else typed them earlier into a file.
With nine or so labels, typing them yourself is quickest.
Note that you type one statement more than you need, even doing it the long way, as you could start
gen div_name = "New England" if div_no == 1
Upvotes: 0