Hi,

I have converted a string variable called last_activity containing dates into a date formatted variable called last_activity_year using the following codes:

Code:
gen last_activity2 = date(last_activity, "MDY")

format %td last_activity2

gen last_activity_year = year(last_activity2)
My data now looks something like this:

Code:
balance    last_activity2
11.38    08jun1965
1602.94    17aug1965
1200 .       17aug1965
12.45        17jan1966
9.07          11apr1967
48.83        25aug1967
9144.33    25sep1967
46.48        31oct1967
38.04       19dec1967
40 .          19dec1967
I want to calculate the sum of balance by month and year. For example, for 19 Dec 1967, I would like to obtain:

Code:
38.04+40 = 78.04
I used the following codes to obtain this but the newly created variable monthly_balance containing the sums is producing incorrect summation output.

Code:
gen last_activity_my=string(month(last_activity2),  "%02.0f")+"_"+string(year(last_activity2))

preserve

collapse (sum) monthly_balance = balance, by(last_activity_my)
Could anyone please help to obtain the desired result? Thanks in advance.