Earlier we created a simple binary variable using the command
generate binalc=alcohol>2
There are two problems with this method. The obvious one is that it only works for creating binary variables. Less obviously, it gives the wrong answer when there are missing observations. In Stata a missing observation behaves like a very large number and so will be recoded to 1 along with the higher levels of alcohol consumption.
There are a number of other useful ways to recode variables
generate agegp=age recode agegp 1=0 2/4=2 5/max=4After this command 1 is recoded to 0, value from 2 to 4 are recoded to 2 and values from 5 to the highest value (6 in this case) are recoded to 4. Any missing values will remain missing. You can also recode a numerical value to missing (indicated by .) or vice versa.
Don't confuse this recode statement with the similar recode function that is also available in Stata. The one given here is the one that the Help system tells you about easily.
xi: logistic case i.age i.alcohol*i.tobaccoThis creates a logistic model with 5 dummy variables for age, 3 each for tobacco and alcohol and 9 for the interaction between them. The xi: command is most useful with model-fitting commands but can be used in other contexts.
tabulate alchohol,generate(alcgp)creates 4 dummary variables called alcgp1,alcgp2, alcgp3, and alcgp4.
egen alctob = group(alcohol tobacco)
The new variable alctob will have values from 1 to 16, indicating the 16 possible combinations of alcohol and tobacco consumption.