Reputation: 915
I have a birthtime
variable:
birthtime
1976m2
1979m8
I ran the following:
gen birthyear=year(birthtime)
gen birthmonth=month(birthtime)
However, the result is nonsense.
Do the year
and month
functions not work for this format?
How can I retrieve year and month from the birthtime
variable?
Upvotes: 1
Views: 79
Reputation: 915
I found that the following does the job.
gen int temp = dofm(birthtime)
gen int birthyear = year(temp)
gen byte birthmonth = month(temp)
The result is
birthyear birthmonth
1976 2
1979 8
Upvotes: 1
Reputation:
You need to study Stata's SIF-to-SIF conversions:
clear
input birthtime
403
404
405
406
407
end
generate birthyear = year(dofm(birthtime))
generate birthmonth = month(dofm(birthtime))
list, abbreviate(10)
+------------------------------------+
| birthtime birthyear birthmonth |
|------------------------------------|
1. | 403 1993 8 |
2. | 404 1993 9 |
3. | 405 1993 10 |
4. | 406 1993 11 |
5. | 407 1993 12 |
+------------------------------------+
Also:
format %tm birthtime
list, abbreviate(10)
+------------------------------------+
| birthtime birthyear birthmonth |
|------------------------------------|
1. | 1993m8 1993 8 |
2. | 1993m9 1993 9 |
3. | 1993m10 1993 10 |
4. | 1993m11 1993 11 |
5. | 1993m12 1993 12 |
+------------------------------------+
Upvotes: 3