In order to extract the month and year components from a variable with the %tm format in Stata, one can use the “month(var)” and “year(var)” commands. These commands will extract the month and year values from the variable specified and store them as separate variables. The month will be represented as a numeric value ranging from 1 to 12, while the year will be represented as a 4-digit number. This method can be useful for analyzing time series data and conducting further statistical analysis on specific time periods within a dataset.
How can I extract month and year component from a variable with %tm format? | Stata FAQ
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.
Cite this article
stats writer (2024). How can I extract the month and year components from a variable with the %tm format in Stata?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-extract-the-month-and-year-components-from-a-variable-with-the-tm-format-in-stata/
stats writer. "How can I extract the month and year components from a variable with the %tm format in Stata?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-extract-the-month-and-year-components-from-a-variable-with-the-tm-format-in-stata/.
stats writer. "How can I extract the month and year components from a variable with the %tm format in Stata?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-extract-the-month-and-year-components-from-a-variable-with-the-tm-format-in-stata/.
stats writer (2024) 'How can I extract the month and year components from a variable with the %tm format in Stata?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-extract-the-month-and-year-components-from-a-variable-with-the-tm-format-in-stata/.
[1] stats writer, "How can I extract the month and year components from a variable with the %tm format in Stata?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I extract the month and year components from a variable with the %tm format in Stata?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
