A variable in %tm format is a numeric variable which is the number of months elapsed since January 1960. One may want to extract the month or the year component from this variable. In this example, we will show how to perform this task by using a function from a suite of date converting functions.
use https://stats.idre.ucla.edu/stat/stata/faq/dow1_monthly, clearlist in 1/10+-------------------------------+ | dm dowclose ln_dow | |-------------------------------| 1. | 1953m1 288.4524 5.664502 | 2. | 1953m2 283.9611 5.648786 | 3. | 1953m3 286.7909 5.658709 | 4. | 1953m4 275.2857 5.617776 | 5. | 1953m5 276.9381 5.623769 | |-------------------------------| 6. | 1953m6 266.8864 5.586792 | 7. | 1953m7 270.3261 5.599611 | 8. | 1953m8 272.2048 5.606421 | 9. | 1953m9 261.9048 5.567924 | 10. | 1953m10 270.7191 5.600968 | +-------------------------------+
Notice that variable dm is in %tm format. If we reformat it as in a general numeric format as shown below, we will see that it is a numeric variable. For example, the number of months from January 1953 to January 1960 is 84. This is what we see as the first observation except that it is negative since it is before January 1960, going backwards.
format dm %10.0gdi (53 - 60)*12 -84list in 1/10+---------------------------+ | dm dowclose ln_dow | |---------------------------| 1. | -84 288.4524 5.664502 | 2. | -83 283.9611 5.648786 | 3. | -82 286.7909 5.658709 | 4. | -81 275.2857 5.617776 | 5. | -80 276.9381 5.623769 | |---------------------------| 6. | -79 266.8864 5.586792 | 7. | -78 270.3261 5.599611 | 8. | -77 272.2048 5.606421 | 9. | -76 261.9048 5.567924 | 10. | -75 270.7191 5.600968 | +---------------------------+
Now how do we extract the month and year information from variable dm? We know that Stata has month() and year() functions available. But we cannot use them yet, since these two functions require that the input be in date format. That is the input variable should also carry the information on day. So we are not quite there yet. But what is also available is the Stata’s suite of converting time format functions. To our end, we can use function dofm().
gen date = dofm(dm)format date %dlist in 1/10+---------------------------------------+ | dm dowclose ln_dow date | |---------------------------------------| 1. | -84 288.4524 5.664502 01jan1953 | 2. | -83 283.9611 5.648786 01feb1953 | 3. | -82 286.7909 5.658709 01mar1953 | 4. | -81 275.2857 5.617776 01apr1953 | 5. | -80 276.9381 5.623769 01may1953 | |---------------------------------------| 6. | -79 266.8864 5.586792 01jun1953 | 7. | -78 270.3261 5.599611 01jul1953 | 8. | -77 272.2048 5.606421 01aug1953 | 9. | -76 261.9048 5.567924 01sep1953 | 10. | -75 270.7191 5.600968 01oct1953 | +---------------------------------------+
Now we are ready to extract month and year information from variable date.
gen month=month(date)gen yr=year(date)list in 1/10+------------------------------------------------------+ | dm dowclose ln_dow date month yr | |------------------------------------------------------| 1. | -84 288.4524 5.664502 01jan1953 1 1953 | 2. | -83 283.9611 5.648786 01feb1953 2 1953 | 3. | -82 286.7909 5.658709 01mar1953 3 1953 | 4. | -81 275.2857 5.617776 01apr1953 4 1953 | 5. | -80 276.9381 5.623769 01may1953 5 1953 | |------------------------------------------------------| 6. | -79 266.8864 5.586792 01jun1953 6 1953 | 7. | -78 270.3261 5.599611 01jul1953 7 1953 | 8. | -77 272.2048 5.606421 01aug1953 8 1953 | 9. | -76 261.9048 5.567924 01sep1953 9 1953 | 10. | -75 270.7191 5.600968 01oct1953 10 1953 | +------------------------------------------------------+
For more information on date conversion functions, type help functions in the Stata command window and click on the link for datetime functions.