1. without strings, use destring or encode, as said above
2. with strings, you'd better be careful. I suggest the following:
2a. check the data to see if there is any error
2b. if there is no error, you can generate a NEW variable that corresponds to the code variable one-on-one. here is how you do it: egen code_new=group(code). Then you can use code_new.