Saturday, November 21, 2020

Generating Monthly variable from Year and Month separate Variables.

Greetings, I've reviewed the Statalist forum in order to seek an answer for my particular case. However, even when there are plenty of similar topics, I couldn't find my specific case to solve the problem.

I got a data set where I extracted the Month and the Year (both numerical) as the next table shows.


Code:
Year    Month_Num
1990    1
1990    2
1990    3
1990    4
1990    5
1990    6

I have tried combining Month_Num and Year into a single variable which is a string variable of the form:


Code:
Year    Month_Num    YM
1990    1                   1-1990
1990    2                   2-1990
1990    3                   3-1990
1990    4                   4-1990
1990    5                   5-1990
1990    6                   6-1990

But when I use
gen date=date(YM, "MY")
format date %tm

It produces results way strange,

Code:
Year    Month_Num    YM    date
1990    1                  1-1990    2873m3
1990    2                  2-1990    2875m10
1990    3                  3-1990    2878m2
1990    4                  4-1990    2880m9
1990    5                  5-1990    2883m3
1990    6                  6-1990    2885m10


I know there should be an easy way by just using the Month_Num and Year variable and put them together and simply format it as time monthly. But I wasn't able to figure out a solution.

Thank you for your time.

No comments:

Post a Comment