Liz's Stata Guide

Favorite Commands and Tricks

This page is dedicated to commands and Stata tricks that I find particularly nifty. Have suggestions of other overlooked jewels of Stata? Email me at djiboliz@gmail.com.

recode

write explanation

numlabel _all, add

My favorite way to deal with category variables (such as district) is to have them stored as numeric variables (usually bytes or ints) with the values labeled. This creates nice tables, etc and avoids having to manipulate strings. Sometimes you need to know the numeric value that corresponds to the label. I used to use
tab district;
tab district, nolabel;
and compare the two tables to find the code for Bonthe. It's much easier to use numlabel which adds the numeric code to the value labels so we can easily see which goes with which.
. numlabel _all, add;

. tab district;

              district |      Freq.     Percent        Cum.
-----------------------+-----------------------------------
          11. Kailahun |        110       10.00       10.00
            12. Kenema |        140       12.73       22.73
              13. Kono |         65        5.91       28.64
           21. Bombali |         80        7.27       35.91
            22. Kambia |         45        4.09       40.00
         23. Koinadugu |         60        5.45       45.45
         24. Port Loko |         80        7.27       52.73
         25. Tonkolili |         60        5.45       58.18
                31. Bo |        110       10.00       68.18
            32. Bonthe |         20        1.82       70.00
           33. Moyamba |         55        5.00       75.00
           34. Pujehun |         40        3.64       78.64
41. Western Area Rural |         60        5.45       84.09
42. Western Area Urban |        175       15.91      100.00
-----------------------+-----------------------------------
                 Total |      1,100      100.00
Note: this adds the numberic codes to all labels (hence _all). If we want to do it for just one variable, we need to know the name of the value label for that variable -- describe will tell us.
. describe district;

              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
district        byte   %18.0g      districts
                                              district

. numlabel districts, add;

contact: djiboliz@gmail.com
last modified: 2 May 2007